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:
Hugo 2017-04-04 01:28:33 +03:00 committed by wiredfool
parent 2152b26515
commit 53df62647a
4 changed files with 32 additions and 3 deletions

View File

@ -119,8 +119,17 @@ def APP(self, marker):
# If DPI isn't in JPEG header, fetch from EXIF
if "dpi" not in self.info and "exif" in self.info:
x_resolution = self._getexif()[0x011A]
self.info["dpi"] = x_resolution[0] / x_resolution[1]
exif = self._getexif()
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):

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -506,7 +506,27 @@ class TestFileJpeg(PillowTestCase):
im = Image.open("Tests/images/photoshop-200dpi.jpg")
# 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__':