diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index 22ac2eaba..bb2816c13 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -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 diff --git a/Tests/images/exif-72dpi-int.jpg b/Tests/images/exif-72dpi-int.jpg new file mode 100644 index 000000000..0b60190d2 Binary files /dev/null and b/Tests/images/exif-72dpi-int.jpg differ diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 9bea7985d..f4cfe8d60 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -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()