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

Reading GIF frame-by-frame changes mode during read in Pillow 9 #5929

Closed
FirefoxMetzger opened this issue Jan 4, 2022 · 2 comments
Closed
Labels

Comments

@FirefoxMetzger
Copy link
Contributor

FirefoxMetzger commented Jan 4, 2022

I encountered some new behavior in pillow v9 that - to me - is a bit counter-intuitive (image attached):

from PIL import Image
import numpy as np

img = Image.open("newtonscradle.gif")

img.seek(0)
foo = np.array(img.getdata())
assert foo.shape == (30000, )
assert img.mode == "P"

img.seek(1)
bar = np.array(img.getdata())

assert bar.shape == (30000, 3)
assert img.mode == "RGB"

Technically, this is not a bug, since it seems to be intended behavior according to the v9 changelog. At the same time, I find it a bit counter-intuitive that the first frame of a GIF now has mode P while each subsequent frame has mode RGB/RGBA.

I was wondering if it would be possible to make this more consistent and also convert the first frame (img.seek(0)) of any GIF to RGB/RGBA, so that the mode of all frames is consistent.

Supplementary:

newtonscradle.gif

@FirefoxMetzger FirefoxMetzger changed the title Reading GIF frame-by-frame changes mode during read starting with pillow v9 Reading GIF frame-by-frame changes mode during read in pillow v9 Jan 4, 2022
@radarhere radarhere changed the title Reading GIF frame-by-frame changes mode during read in pillow v9 Reading GIF frame-by-frame changes mode during read in Pillow 9 Jan 4, 2022
@radarhere radarhere added the GIF label Jan 4, 2022
@radarhere
Copy link
Member

I presume there is a contingent of Pillow users who just read the first frame of images, and don't bother seeking past that. #5857 had no effect on those users.

For Pillow users who do read animated GIFs, I expect them to be ok with this change, in the face of being able to read many GIF images correctly for the first time.

This is just a judgement call, but for my part, I'm a bit reluctant to change the experience of the first group of users for something that isn't an error. P mode more accurately reflects what is in that first image.

@FirefoxMetzger
Copy link
Contributor Author

FirefoxMetzger commented Jan 7, 2022

For Pillow users who do read animated GIFs, I expect them to be ok with this change, in the face of being able to read many GIF images correctly for the first time.

I, too, like the idea behind this change, because it means better support for GIFs that use a different palette for each image and try to be smart on what areas they do/don't dispose of.

I'm not sure I agree on the part where P is the more accurate mode for the first image, though. RGB/RGBA seems to be more desirable for subsequent images (hence the change), and I don't see what makes the first page special to warrant an exception. Then again, you probably have a better view of the broader context, and it might indeed be the better choice. To me, this behavior was/is still quite surprising and a bit unexpected, which is why I opened this issue to ask if it is worthwhile to change it.

Since you don't seem to think that it is worthwhile to change this behavior, I will go ahead and close this issue and look into changes downstream to adjust for the new pillow behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants