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

Consider all frames when selecting mode for PNG save_all #6610

Merged
merged 1 commit into from Oct 18, 2022

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Sep 23, 2022

Resolves #6608

At the moment, when saving multiple frames with PngImagePlugin, the mode of the first image is used.

mode = im.mode

However, this is not necessarily ideal. For a GIF, the first frame might be P, and subsequent frames might be RGB. Converting the RGB frames to P could result in loss of information.

This PR loops through the frames to be saved, checks if any are RGB, RGBA or P, and upgrades the mode accordingly.

@seidnerj
Copy link

seidnerj commented Oct 4, 2022

This fix seems to not be working correctly for some files. See the attached animated GIF and the resulting animated PNG. Some of the frames in the PNG file are corrupted.

bB4Y4h
bB4Y4h

@seidnerj
Copy link

seidnerj commented Oct 4, 2022

See attached a corrupted frame and the corresponding original frame (albeit with fewer colors than in the original animated GIF since there are only 256 available in a single frame).

original frame
corrupted frame

@radarhere
Copy link
Member Author

That actually isn't a problem related to this PR - you've found a GIF that Pillow isn't reading correctly in the first place.

Run this code over your GIF, and you will see what I'm talking about.

from PIL import Image
im = Image.open("in.gif")
im.seek(3)
im.save("out.gif")

I'll investigate and see if I can put together a fix in a new PR.

@seidnerj
Copy link

seidnerj commented Oct 5, 2022

Just my luck, running into 3 separate issues all with the same set of GIFs 🤦‍♂️

Thanks a lot!

@radarhere
Copy link
Member Author

I've added a commit to #6576 to fix your GIF.

@seidnerj
Copy link

seidnerj commented Oct 6, 2022

Many thanks!

@hugovk hugovk merged commit 3d49166 into python-pillow:main Oct 18, 2022
60 checks passed
@radarhere radarhere deleted the png_save_all branch October 18, 2022 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image.show() defaults to PNG ignoring animated GIFs
3 participants