Fix for file with DPI in EXIF but not metadata, and XResolution is an int rather than tuple

This commit is contained in:
Hugo 2017-04-11 14:07:31 +03:00 committed by wiredfool
parent e7f156c909
commit 926a778d40
3 changed files with 18 additions and 4 deletions

View File

@ -123,7 +123,10 @@ def APP(self, marker):
try:
resolution_unit = exif[0x0128]
x_resolution = exif[0x011A]
dpi = x_resolution[0] / x_resolution[1]
try:
dpi = x_resolution[0] / x_resolution[1]
except TypeError:
dpi = x_resolution
if resolution_unit == 3: # cm
# 1 dpcm = 2.54 dpi
dpi *= 2.54

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

View File

@ -501,14 +501,24 @@ class TestFileJpeg(PillowTestCase):
reloaded.load()
self.assertEqual(im.info['dpi'], reloaded.info['dpi'])
def test_dpi_from_exif(self):
def test_dpi_tuple_from_exif(self):
# Arrange
# This Photoshop CC 2017 image has DPI in EXIF not metadata
# EXIF XResolution is (2000000, 10000)
im = Image.open("Tests/images/photoshop-200dpi.jpg")
# Act / Assert
self.assertEqual(im.info.get("dpi"), (200, 200))
def test_dpi_int_from_exif(self):
# Arrange
# This image has DPI in EXIF not metadata
# EXIF XResolution is 72
im = Image.open("Tests/images/exif-72dpi-int.jpg")
# Act / Assert
self.assertEqual(im.info.get("dpi"), (72, 72))
def test_dpi_from_dpcm_exif(self):
# Arrange
# This is photoshop-200dpi.jpg with EXIF resolution unit set to cm:
@ -535,7 +545,7 @@ class TestFileCloseW32(PillowTestCase):
def setUp(self):
if "jpeg_encoder" not in codecs or "jpeg_decoder" not in codecs:
self.skipTest("jpeg support not available")
def test_fd_leak(self):
tmpfile = self.tempfile("temp.jpg")
import os
@ -549,8 +559,9 @@ class TestFileCloseW32(PillowTestCase):
self.assertRaises(Exception, lambda: os.remove(tmpfile))
im.load()
self.assertTrue(fp.closed)
# this should not fail, as load should have closed the file.
# this should not fail, as load should have closed the file.
os.remove(tmpfile)
if __name__ == '__main__':
unittest.main()