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

GIF palettes #3735

Closed
radarhere opened this issue Mar 22, 2019 · 3 comments
Closed

GIF palettes #3735

radarhere opened this issue Mar 22, 2019 · 3 comments

Comments

@radarhere
Copy link
Member

radarhere commented Mar 22, 2019

It is apparent from various reported issues that Pillow has a problem with the palettes on subsequent GIF frames. To use one of Pillow's own test images to demonstrate -

dispose_bgnd

from PIL import Image
im = Image.open("Tests/images/dispose_bgnd.gif")
im.seek(2)
im.save("out.png")

out

Each GIF frame can have its own palette, and Pillow is reading that in as the palette that of that new frame. However, when the new frame is at least partially pasted on top of the existing image, the palette should not be applied to the pixels already loaded. So the image has turned red here because the blue in the previous palette is red in the new palette.

I put together a change to combine each of the palettes as they were loaded, but discovered that this doesn't work as well as it could because of the limitation of 256 colours in a palette.

Another option is to convert each of the image parts from P to RGB for merging and then back again, but the image then becomes an approximation of an image where users aren't able to know for certain what colour a pixel was in the file.

The most straightforward solution to this is to change GIF P images to RGB and leave them that way, removing the limitation of 256 colours. However, considering Pillow's strong encouragement of backwards compatibility, this is might be considered a bit of a problem.

So, any thoughts?

@dhsdshdhk
Copy link

Not answering your question, just wondering, there can only be 256 different pixels in a frame?

@radarhere
Copy link
Member Author

https://www.w3.org/Graphics/GIF/spec-gif87.txt

The value of 'pixel' also defines the maximum number of colors within an image. The range of values for 'pixel' is 0 to 7 which represents 1 to 8 bits. This translates to a range of 2 (B & W) to 256 colors.

@radarhere
Copy link
Member Author

I eventually went with this idea, and created #5857.

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

No branches or pull requests

2 participants