diff --git a/Tests/images/padded_idat.png b/Tests/images/padded_idat.png new file mode 100644 index 00000000000..18c5a4990cd Binary files /dev/null and b/Tests/images/padded_idat.png differ diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 15d74ca6b9e..0356873afee 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -628,6 +628,23 @@ def test_textual_chunks_after_idat(self): with Image.open("Tests/images/hopper_idat_after_image_end.png") as im: assert im.text == {"TXT": "VALUE", "ZIP": "VALUE"} + def test_padded_idat(self): + # This image has been manually hexedited + # so that the IDAT chunk has padding at the end + # Set MAXBLOCK to the length of the actual data + # so that the decoder finishes reading before the chunk ends + MAXBLOCK = ImageFile.MAXBLOCK + ImageFile.MAXBLOCK = 45 + ImageFile.LOAD_TRUNCATED_IMAGES = True + + with Image.open("Tests/images/padded_idat.png") as im: + im.load() + + ImageFile.MAXBLOCK = MAXBLOCK + ImageFile.LOAD_TRUNCATED_IMAGES = False + + assert_image_equal_tofile(im, "Tests/images/bw_gradient.png") + def test_specify_bits(self, tmp_path): im = hopper("P") diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 07bbc52280f..8f0d566d177 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -920,6 +920,8 @@ def load_read(self, read_bytes): def load_end(self): """internal: finished reading image data""" + if self.__idat != 0: + self.fp.read(self.__idat) while True: self.fp.read(4) # CRC