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

Only compare to previous frame when checking for duplicate GIF frames while saving #6787

Merged
merged 2 commits into from
Dec 23, 2022

Conversation

radarhere
Copy link
Member

Resolves #6785

When saving GIFs, a disposal value of 2 means that after the frame, we should "Restore to background color".

For that scenario, GifImagePlugin correctly compares the new frame to the background color, so that the new frame is optimised by only including the parts that differ from the background color.

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)
base_im = background_im
else:
base_im = previous["im"]
if _get_palette_bytes(im_frame) == _get_palette_bytes(base_im):
delta = ImageChops.subtract_modulo(im_frame, base_im)
else:
delta = ImageChops.subtract_modulo(
im_frame.convert("RGB"), base_im.convert("RGB")
)
bbox = delta.getbbox()

However, GifImagePlugin then decides that if there is no difference, the frame is identical to the previous frame.

if not bbox:
# This frame is identical to the previous frame

That is not true when disposal is 2. It would be true if disposal weren't 2, and we were comparing this frame to the previous frame, but we are instead comparing it to the background color.

So this PR instead only compares the previous frame with this frame when checking for duplicate frames.

@radarhere radarhere changed the title Only compare to previous when checking for duplicate GIF frames while saving Only compare to previous frame when checking for duplicate GIF frames while saving Dec 8, 2022
@hugovk hugovk merged commit 7a19251 into python-pillow:main Dec 23, 2022
@radarhere radarhere deleted the gif_disposal branch December 23, 2022 20:11
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.

A frame is *ignored* when a GIF is saved with a completely transparent frame included
2 participants