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 images incorrectly converted to RGB #434

Closed
cezarsa opened this issue Dec 4, 2013 · 9 comments
Closed

GIF images incorrectly converted to RGB #434

cezarsa opened this issue Dec 4, 2013 · 9 comments
Labels
Bug Any unexpected behavior, until confirmed feature.
Milestone

Comments

@cezarsa
Copy link
Contributor

cezarsa commented Dec 4, 2013

After git bisecting the code I found that starting from commit b9ab3f5 there is a issue when converting GIFs from "P" format to either "RGB" or "RGBA".
In this case, pillow generates a black and white only image as it can be seen further down.
I'll try to understand what's happening and make a pull request latter.

This little snippet shows a way to reproduce it:

from PIL import Image
from urllib2 import urlopen
from io import BytesIO

imgbuffer = urlopen('http://www.eecs.berkeley.edu/~loarie/test.colors.gif').read()

img = Image.open(BytesIO(imgbuffer))
rgbimg = img.convert('RGB')
rgbimg.save(open('tmp.gif', 'w'), 'GIF')

Result:
tmp

Expected:
original

cc: commit author @d-schmidt

@wiredfool
Copy link
Member

I think that this is the same as #403, which was localized to the same commit. Can you check head to see if the patch in #405 is a fix?

@d-schmidt
Copy link
Contributor

No the problem here lies in the automatic P conversion on save. #405 seems to change the result, at least my bugged image looks different but it doesn't solve this problem.

@d-schmidt
Copy link
Contributor

We can remove the "feature" maintaining the old palette but it bloats the result file. I'm checking how to solve it properly and I'm currently stumbling across the optimization.

@cezarsa
Copy link
Contributor Author

cezarsa commented Dec 4, 2013

After going through the code I think the best thing to do would be to revert the offending commit for now.

The biggest issue I found is that it's not safe to reuse the original palette after calling convert() on the image. The problem is that although convert() preserves the original self.palette it's no longer valid because as part of the conversion process a new palette is generated and the image pixels use this new information.

I think a full solution to reuse the palette would need to address it during the quantization phase of the conversion, limiting the possible colors to the ones already on the palette. But it seems a much more complex thing to do.

@d-schmidt
Copy link
Contributor

Please try https://github.com/d-schmidt/Pillow/commit/ac6a731bb905a2dc591a921df2b421c7f28dd597
It is not yet ready to merge until we've checked what happens to L-mode and transparent gifs.

new result:
test3
Notice this is different on binary level. Colors switched places in the palette going through two conversions and optimization.

from PIL import Image
f = open("testbild/test.colors.gif", "rb")
i = Image.open(f)
i.save("test1.gif")
i.save("test2.gif") # check optimization result
i = i.convert("RGB")
i.save("test3.gif") # check automatic P conversion

@cezarsa
Copy link
Contributor Author

cezarsa commented Dec 4, 2013

I think moving im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True) to right after the automatic conversion would fix it for L GIFs. Currently it's broken.

@d-schmidt
Copy link
Contributor

It is there to prevent the gif lib from saving the complete WEB gif palette if you just open and save a new gif. I don''t know yet why it is done at all. Imo it should use the loaded palette from the file.

Will put some more time into it tomorrow.

@d-schmidt
Copy link
Contributor

Or next year. This is not forgotten but I'm on a tight schedule atm.

@aclark4life
Copy link
Member

Now targeting 2.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

No branches or pull requests

4 participants