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

gif frame deduplication on existing files without directly specifying duration results in incorrectly altered gif frame durations #6778

Closed
katanacrimson opened this issue Dec 5, 2022 · 3 comments · Fixed by #6779
Labels

Comments

@katanacrimson
Copy link

katanacrimson commented Dec 5, 2022

In relation to @radarhere's comment here, I've discovered an edge case that does not fall into the behavior described.

What did you do?

But what you might notice in the above is that Pillow combines the duration of the frames. So if you add in duration to your code, then when opening the image back up again, you can detect the duration and use that to determine the original binary string.

I'm running into a similar issue as the reporter of #5928 and I believe I've found an edge case that doesn't follow the desired frame deduplication behavior.

First, let's presume we have a pre-existing animated gif file we open with Image.open. This gif already has durations specified and has duplicate frames, each with their own durations. Now, in the process of attempting to generate a thumbnail and perform some operations on the file, let's assume we save the file with save_all=True, but without specifying a duration because we want to leave the original file/frame durations unaltered.

Going by the state of main (currently 6a2545f6) and following the code, we're able to set the original frame's durations by falling back on the im_frame.info["duration"] as seen here:

elif duration is None and "duration" in im_frame.info:
encoderinfo["duration"] = im_frame.info["duration"]

...but when the deduplication check is performed, the same pattern of falling back on the frame's duration is not used:

if duration:
previous["encoderinfo"]["duration"] += encoderinfo["duration"]

As duration is none during the frame deduplication, it drops the duplicates but it never combines the durations into the prior frames. This ultimately results in an altered animation with notably different timings than the original.

At best guess, a fix might be to check if encoderinfo["duration"] instead of if duration, but I'm very unfamiliar with the codebase and don't have much confidence in that recommendation.

(Apologies, I cannot currently provide a sample of the image to help reproduce this bug. If I have time later, I'll try to mock one out with another tool to help reproduce the issue.)

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04
  • Python: 3.10.6
  • Pillow: 9.3.0
@katanacrimson
Copy link
Author

For others, my workaround when saving -

durations = []
for i in range(0, img.n_frames):
    img.seek(i)
    durations.append(img.info['duration'])
img.save(dest, save_all=True, duration=durations)

@radarhere
Copy link
Member

I've created PR #6779 to resolve this, using your suggestion (fractionally modified).

@katanacrimson
Copy link
Author

katanacrimson commented Dec 5, 2022

Cool, good to know I wasn't far off (and that I was coherent enough with the late-night bug report, lol). Thank you for the PR!

hugovk added a commit that referenced this issue Dec 13, 2022
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 a pull request may close this issue.

2 participants