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

When saving GIF frame that restores to background color, do not fill identical pixels #7788

Merged
merged 2 commits into from Mar 11, 2024

Conversation

radarhere
Copy link
Member

Resolves #7787

#7568 set identical pixels in subsequent GIF frames to be transparency.

This new issue has found that for disposal 2, when GIF disposes the previous frame and restores it to the background color, filling the identical pixels with transparency results in those pixels being seen as transparent by viewers.

This PR skips the optimisation step of filling identical pixels when the previous frame is restored to the background colour, as it is unnecessary when the frame is being compared with a single color (the background). The filling of pixels that match the previous frame is only an optimisation when it is reducing multiple possible matching colors to one color.

@radarhere
Copy link
Member Author

In my first commit here, I used the previous frame's disposal when considered whether the current frame should be compared to the background color or not.

However, the current frame's disposal was being used when generating background color frame for comparison.

if encoderinfo.get("disposal") == 2:
if background_im is None:
color = im.encoderinfo.get(
"transparency", im.info.get("transparency", (0, 0, 0))
)
background = _get_background(im_frame, color)
background_im = Image.new("P", im_frame.size, background)
background_im.putpalette(im_frames[0]["im"].palette)
delta, bbox = _getbbox(background_im, im_frame)

This is incorrect, and so I've pushed another commit with a new test. Disposal refers to how the current frame will be treated when you have moved on to the next one - how the current frame is disposed of.

@hugovk hugovk merged commit 93bf991 into python-pillow:main Mar 11, 2024
56 checks passed
@radarhere radarhere deleted the gif_disposal branch March 12, 2024 00: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.

Setting disposal=2 on gif export can cause issues with transparency
2 participants