Fix ZeroDivisionError when EXIF contains invalid DPI (0/0).

This commit is contained in:
Vytis Banaitis 2017-08-09 16:16:14 +03:00
parent cae7789372
commit 7c8e0e4457
3 changed files with 12 additions and 1 deletions

View File

@ -131,9 +131,10 @@ def APP(self, marker):
# 1 dpcm = 2.54 dpi
dpi *= 2.54
self.info["dpi"] = dpi, dpi
except (KeyError, SyntaxError):
except (KeyError, SyntaxError, ZeroDivisionError):
# SyntaxError for invalid/unreadable exif
# KeyError for dpi not included
# ZeroDivisionError for invalid dpi rational value
self.info["dpi"] = 72, 72

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -535,6 +535,16 @@ class TestFileJpeg(PillowTestCase):
# Act / Assert
self.assertEqual(im.info.get("dpi"), (508, 508))
def test_dpi_exif_zero_division(self):
# Arrange
# This is photoshop-200dpi.jpg with EXIF resolution set to 0/0:
# exiftool -XResolution=0/0 -YResolution=0/0 photoshop-200dpi.jpg
im = Image.open("Tests/images/exif-dpi-zerodivision.jpg")
# Act / Assert
# This should return the default, and not raise a ZeroDivisionError
self.assertEqual(im.info.get("dpi"), (72, 72))
def test_no_dpi_in_exif(self):
# Arrange
# This is photoshop-200dpi.jpg with resolution removed from EXIF: