png: initialize _n_frames

- ImageFile._seek_check() will not EOF unless _n_frames is properly set
  for standard (non-animated) PNG images
This commit is contained in:
Peter Rowlands 2020-04-03 20:34:22 +09:00
parent 8c9100e267
commit dc56c6064f
2 changed files with 9 additions and 2 deletions

View File

@ -635,7 +635,10 @@ class TestFilePng:
def test_seek(self):
with Image.open(TEST_PNG_FILE) as im:
assert im.n_frames == 1
im.seek(0)
with pytest.raises(EOFError):
im.seek(1)
@pytest.mark.skipif(is_win32(), reason="Requires Unix or macOS")

View File

@ -636,7 +636,6 @@ class PngImageFile(ImageFile.ImageFile):
if self.fp.read(8) != _MAGIC:
raise SyntaxError("not a PNG file")
self.__fp = self.fp
self.__frame = 0
#
# Parse headers up to the first IDAT or fDAT chunk
@ -685,7 +684,12 @@ class PngImageFile(ImageFile.ImageFile):
else:
self.__prepare_idat = length # used by load_prepare()
if self._n_frames is not None:
if self._n_frames is None:
# standard PNG image, frame-related attributes still need to be
# initialized for seek()/_seek_check() to behave as expected
self.__frame = 0
self._n_frames = 1
else:
self._close_exclusive_fp_after_loading = False
self.png.save_rewind()
self.__rewind_idat = self.__prepare_idat