Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not update GIF frame position until local image is found #6219

Merged
merged 1 commit into from May 27, 2022

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Apr 16, 2022

Resolves #6320

#6207 (reply in thread) points out that if a GIF file contains a comment block after the last image, then Pillow calculates n_frames incorrectly, thinking there is 1 more frame than there really is.

Why does this happen? n_frames seeks through the image until it hits an EOFError.

def n_frames(self):
if self._n_frames is None:
current = self.tell()
try:
while True:
self._seek(self.tell() + 1, False)
except EOFError:
self._n_frames = self.tell() + 1
self.seek(current)
return self._n_frames

At the moment, once GifImagePlugin determines that there is more data in the file, it updates the frame position.

s = self.fp.read(1)
if not s or s == b";":
raise EOFError
self.__frame = frame

It doesn't consider that an EOFError might still be raised if the local image is not found.

if interlace is None:
# self.__fp = None
raise EOFError

where interlace is only set when reading the local image.

So this PR just moves self.__frame = frame after that last EOFError.

The test image was created using a modified Pillow.

@radarhere radarhere added the GIF label Apr 16, 2022
@hugovk hugovk merged commit 0476914 into python-pillow:main May 27, 2022
@radarhere radarhere deleted the gif_eof branch May 27, 2022 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: n_frames is incorrect
2 participants