mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-12 10:16:17 +03:00
DPI is a tuple (#2472)
* DPI is a tuple * Some EXIF only contains an X resolution for DPI * Refactor * Test with no DPI in EXIF * Handle EXIF with no DPI * Created with: exiftool "-*resolution*"= photoshop-200dpi.jpg * Test when not in EXIF, DPI==72,72 * Use X resolution for Y, default to 72,72 dpi * Created with: exiftool -exif:ResolutionUnit=cm photoshop-200dpi.jpg * Test for EXIF with dpcm instead of dpi * Convert dpcm to dpi, and default to inches if unit unknown
This commit is contained in:
parent
2152b26515
commit
53df62647a
|
@ -119,8 +119,17 @@ def APP(self, marker):
|
||||||
|
|
||||||
# If DPI isn't in JPEG header, fetch from EXIF
|
# If DPI isn't in JPEG header, fetch from EXIF
|
||||||
if "dpi" not in self.info and "exif" in self.info:
|
if "dpi" not in self.info and "exif" in self.info:
|
||||||
x_resolution = self._getexif()[0x011A]
|
exif = self._getexif()
|
||||||
self.info["dpi"] = x_resolution[0] / x_resolution[1]
|
try:
|
||||||
|
resolution_unit = exif[0x0128]
|
||||||
|
x_resolution = exif[0x011A]
|
||||||
|
dpi = x_resolution[0] / x_resolution[1]
|
||||||
|
if resolution_unit == 3: # cm
|
||||||
|
# 1 dpcm = 2.54 dpi
|
||||||
|
dpi *= 2.54
|
||||||
|
self.info["dpi"] = dpi, dpi
|
||||||
|
except KeyError:
|
||||||
|
self.info["dpi"] = 72, 72
|
||||||
|
|
||||||
|
|
||||||
def COM(self, marker):
|
def COM(self, marker):
|
||||||
|
|
BIN
Tests/images/exif-200dpcm.jpg
Normal file
BIN
Tests/images/exif-200dpcm.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
BIN
Tests/images/no-dpi-in-exif.jpg
Normal file
BIN
Tests/images/no-dpi-in-exif.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
|
@ -506,7 +506,27 @@ class TestFileJpeg(PillowTestCase):
|
||||||
im = Image.open("Tests/images/photoshop-200dpi.jpg")
|
im = Image.open("Tests/images/photoshop-200dpi.jpg")
|
||||||
|
|
||||||
# Act / Assert
|
# Act / Assert
|
||||||
self.assertEqual(im.info.get("dpi"), 200)
|
self.assertEqual(im.info.get("dpi"), (200, 200))
|
||||||
|
|
||||||
|
def test_dpi_from_dpcm_exif(self):
|
||||||
|
# Arrange
|
||||||
|
# This is photoshop-200dpi.jpg with EXIF resolution unit set to cm:
|
||||||
|
# exiftool -exif:ResolutionUnit=cm photoshop-200dpi.jpg
|
||||||
|
im = Image.open("Tests/images/exif-200dpcm.jpg")
|
||||||
|
|
||||||
|
# Act / Assert
|
||||||
|
self.assertEqual(im.info.get("dpi"), (508, 508))
|
||||||
|
|
||||||
|
def test_no_dpi_in_exif(self):
|
||||||
|
# Arrange
|
||||||
|
# This is photoshop-200dpi.jpg with resolution removed from EXIF:
|
||||||
|
# exiftool "-*resolution*"= photoshop-200dpi.jpg
|
||||||
|
im = Image.open("Tests/images/no-dpi-in-exif.jpg")
|
||||||
|
|
||||||
|
# Act / Assert
|
||||||
|
# "When the image resolution is unknown, 72 [dpi] is designated."
|
||||||
|
# http://www.exiv2.org/tags.html
|
||||||
|
self.assertEqual(im.info.get("dpi"), (72, 72))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user