Merge pull request #2365 from SemanticsOS/lambdafu/dpiresolution

Default to inch-interpretation for missing ResolutionUnit in TiffImagePlugin
This commit is contained in:
wiredfool 2017-01-26 14:22:32 +00:00 committed by GitHub
commit a62f2c5b44
2 changed files with 23 additions and 1 deletions

View File

@ -1163,11 +1163,15 @@ class TiffImageFile(ImageFile.ImageFile):
yres = self.tag_v2.get(Y_RESOLUTION, 1)
if xres and yres:
resunit = self.tag_v2.get(RESOLUTION_UNIT, 1)
resunit = self.tag_v2.get(RESOLUTION_UNIT)
if resunit == 2: # dots per inch
self.info["dpi"] = xres, yres
elif resunit == 3: # dots per centimeter. convert to dpi
self.info["dpi"] = xres * 2.54, yres * 2.54
elif resunit == None: # used to default to 1, but now 2)
self.info["dpi"] = xres, yres
# For backward compatibility, we also preserve the old behavior.
self.info["resolution"] = xres, yres
else: # No absolute unit of measurement
self.info["resolution"] = xres, yres

View File

@ -93,6 +93,24 @@ class TestFileTiff(PillowTestCase):
self.assertEqual(im.info['dpi'], (72., 72.))
def test_xyres_fallback_tiff(self):
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION, RESOLUTION_UNIT
filename = "Tests/images/compression.tif"
im = Image.open(filename)
# v2 api
self.assertIsInstance(im.tag_v2[X_RESOLUTION],
TiffImagePlugin.IFDRational)
self.assertIsInstance(im.tag_v2[Y_RESOLUTION],
TiffImagePlugin.IFDRational)
self.assertRaises(KeyError,
lambda: im.tag_v2[RESOLUTION_UNIT])
# Legacy.
self.assertEqual(im.info['resolution'], (100., 100.))
# Fallback "inch".
self.assertEqual(im.info['dpi'], (100., 100.))
def test_int_resolution(self):
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION
filename = "Tests/images/pil168.tif"