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

Improve transparency handling when saving GIF images #6176

Merged
merged 3 commits into from May 27, 2022

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Apr 2, 2022

Resolves #6162
Resolves #6260

When saving a GIF, _normalize_mode may set info["transparency"] for a frame.

im.info["transparency"] = im.palette.colors[rgba]

When the code comes back to _write_multiple_frames, the transparency is used, if the user hasn't overridden it from encoderinfo... but it is only copied from the first frame.

im_frame = _normalize_mode(im_frame.copy())
if frame_count == 0:
for k, v in im_frame.info.items():
im.encoderinfo.setdefault(k, v)

This PR allows each frame to have a different transparency from _normalize_mode.

@raygard
Copy link
Contributor

raygard commented May 2, 2022

Where you now have:

if "transparency" in im_frame.info:
	encoderinfo.setdefault("transparency", im_frame.info["transparency"])
im_frame = _normalize_palette(im_frame, palette, encoderinfo)

Does it hurt anything to put the im_frame = ... in front?

im_frame = _normalize_palette(im_frame, palette, encoderinfo)
if "transparency" in im_frame.info:
	encoderinfo.setdefault("transparency", im_frame.info["transparency"])

I ask because it will help my issue #6260 with transparency if they can be swapped around like this.

@radarhere
Copy link
Member Author

_remap_palette, as the name suggests, reorders the palette. However, it doesn't update the transparency index, meaning that they can become out of sync. This is what happens in #6260. I've added a commit here to fix that.

Also using the swap mentioned in the previous comment, that means that when _normalize_palette calls _remap_palette, the updated transparency can then be used for the frame.

@radarhere radarhere changed the title Consider transparency from each frame when saving GIF Improve transparency handling when saving GIF May 21, 2022
@raygard
Copy link
Contributor

raygard commented May 23, 2022

Looks good! Your approach is much cleaner than mine was. I hope this can get into the next release.

@hugovk hugovk merged commit 27f5c4d into python-pillow:main May 27, 2022
@radarhere radarhere deleted the gif_transparency branch May 27, 2022 08:57
@radarhere radarhere changed the title Improve transparency handling when saving GIF Improve transparency handling when saving GIF images May 27, 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 this pull request may close these issues.

GIF has transparency issues, not all fixed with PR #6176 Background is white instead of being transparent
3 participants