diff --git a/Tests/images/photoshop-200dpi-broken.jpg b/Tests/images/photoshop-200dpi-broken.jpg new file mode 100644 index 000000000..a574872f2 Binary files /dev/null and b/Tests/images/photoshop-200dpi-broken.jpg differ diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index e43b27f52..72adbe537 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -659,6 +659,11 @@ class TestFileJpeg(PillowTestCase): }, ) + # Test that the image can still load, even with broken Photoshop data + # This image had the APP13 length hexedited to be smaller + with Image.open("Tests/images/photoshop-200dpi-broken.jpg") as im_broken: + self.assert_image_equal(im_broken, im) + # This image does not contain a Photoshop header string with Image.open("Tests/images/app13.jpg") as im: self.assertNotIn("photoshop", im.info) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index ea2f795a7..4286e1ae7 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -109,7 +109,10 @@ def APP(self, marker): while blocks[offset : offset + 4] == b"8BIM": offset += 4 # resource code - code = i16(blocks, offset) + try: + code = i16(blocks, offset) + except struct.error: + break offset += 2 # resource name (usually empty) name_len = i8(blocks[offset])