Skip to content

Conversation

@radarhere
Copy link
Member

Resolves #8746

When GIF frames are saved, Pillow crops out parts of frames i order to save space.

In #5557, I found https://legacy.imagemagick.org/Usage/anim_basics/#dispose. In the 'Dispose Background' section (a.k.a. disposal method 2), it states that

As you can see as each overlaid frame is disposed of, that frames area is cleared to transparency, before the next image is overlaid.
...
There is some thinking that rather than clearing the overlaid area to the transparent color, this disposal should clear it to the 'background' color meta-data setting stored in the GIF animation. In fact the old "Netscape" browser (version 2 and 3), did exactly that. But then it also failed to implement the 'Previous' dispose method correctly.
On the other hand the initial canvas should also be set from the formats 'background' color too, and that is also not done. However all modern web browsers clear just the area that was last overlaid to transparency, as such this is now accepted practice, and what IM now follows.

So there is apparently a convention to clear to transparency for disposal method 2, rather than the background colour as described by the specification. The new issue has found this to be a problem with our saved GIFs.

To fix this, when saving with disposal method 2, this PR will only crop if there is transparency.

@radarhere radarhere added the GIF label Feb 12, 2025
@hugovk hugovk merged commit 3f111b9 into python-pillow:main Feb 17, 2025
52 checks passed
@radarhere radarhere deleted the gif branch February 17, 2025 20:50
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.

GIF encoder: black pixels incorrectly cropped when disposal=2

2 participants