diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index a84d01755..7b23df392 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -208,6 +208,8 @@ OPEN_INFO = { PREFIXES = [b"MM\000\052", b"II\052\000", b"II\xBC\000"] +FLOAT_MIN = sys.float_info.min * sys.float_info.epsilon + def _accept(prefix): return prefix[:4] in PREFIXES @@ -477,7 +479,7 @@ class ImageFileDirectory_v2(collections.MutableMapping): @_register_loader(5, 8) def load_rational(self, data, legacy_api=True): vals = self._unpack("{0}L".format(len(data) // 4), data) - combine = lambda a, b: (a, b) if legacy_api else a / b + combine = lambda a, b: (a, b) if legacy_api else a / (b or FLOAT_MIN) return tuple(combine(num, denom) for num, denom in zip(vals[::2], vals[1::2])) diff --git a/Tests/images/exif_zerodivisionerror.jpg b/Tests/images/exif_zerodivisionerror.jpg new file mode 100644 index 000000000..2715215a2 Binary files /dev/null and b/Tests/images/exif_zerodivisionerror.jpg differ diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 367e57c14..a4949d4d7 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -177,6 +177,12 @@ class TestFileJpeg(PillowTestCase): # Should not raise a TypeError im._getexif() + def test_exif_zerodivisionerror(self): + im = Image.open('Tests/images/exif_zerodivisionerror.jpg') + + # Should not raise a ZeroDivisionError + im._getexif() + def test_progressive_compat(self): im1 = self.roundtrip(hopper()) im2 = self.roundtrip(hopper(), progressive=1)