Merge pull request #2632 from wiredfool/issue_2628

Fix JPEG DPI when EXIF is invalid
This commit is contained in:
Hugo 2017-07-25 11:01:29 +03:00 committed by GitHub
commit 90886a4e59
3 changed files with 13 additions and 2 deletions

View File

@ -119,8 +119,8 @@ 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:
exif = self._getexif()
try:
exif = self._getexif()
resolution_unit = exif[0x0128]
x_resolution = exif[0x011A]
try:
@ -131,7 +131,9 @@ def APP(self, marker):
# 1 dpcm = 2.54 dpi
dpi *= 2.54
self.info["dpi"] = dpi, dpi
except KeyError:
except (KeyError, SyntaxError):
# SyntaxError for invalid/unreadable exif
# KeyError for dpi not included
self.info["dpi"] = 72, 72

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -546,6 +546,15 @@ class TestFileJpeg(PillowTestCase):
# http://www.exiv2.org/tags.html
self.assertEqual(im.info.get("dpi"), (72, 72))
def test_invalid_exif(self):
# This is no-dpi-in-exif with the tiff header of the exif block
# hexedited from MM * to FF FF FF FF
im = Image.open("Tests/images/invalid-exif.jpg")
# This should return the default, and not a SyntaxError or
# OSError for unidentified image.
self.assertEqual(im.info.get("dpi"), (72, 72))
@unittest.skipUnless(sys.platform.startswith('win32'), "Windows only")
class TestFileCloseW32(PillowTestCase):