diff --git a/Tests/images/truncated_exif_dpi.jpg b/Tests/images/truncated_exif_dpi.jpg new file mode 100644 index 00000000000..b41ab400442 Binary files /dev/null and b/Tests/images/truncated_exif_dpi.jpg differ diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 769d7ed969a..a0822d0002c 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -767,6 +767,13 @@ def test_dpi_exif_string(self): # This should return the default assert im.info.get("dpi") == (72, 72) + def test_dpi_exif_truncated(self): + # Arrange + with Image.open("Tests/images/truncated_exif_dpi.jpg") as im: + # Act / Assert + # This should return the default + assert im.info.get("dpi") == (72, 72) + def test_no_dpi_in_exif(self): # Arrange # This is photoshop-200dpi.jpg with resolution removed from EXIF: diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 2bb10e1f695..917bbf39fbb 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -170,11 +170,19 @@ def APP(self, marker): # 1 dpcm = 2.54 dpi dpi *= 2.54 self.info["dpi"] = dpi, dpi - except (TypeError, KeyError, SyntaxError, ValueError, ZeroDivisionError): - # SyntaxError for invalid/unreadable EXIF + except ( + struct.error, + KeyError, + SyntaxError, + TypeError, + ValueError, + ZeroDivisionError, + ): + # struct.error for truncated EXIF # KeyError for dpi not included - # ZeroDivisionError for invalid dpi rational value + # SyntaxError for invalid/unreadable EXIF # ValueError or TypeError for dpi being an invalid float + # ZeroDivisionError for invalid dpi rational value self.info["dpi"] = 72, 72