[Image-SIG] colorshift while converting png to gif

Henryk Gerlach hgerlach at compals.com
Wed Feb 9 14:34:07 CET 2005


Hi Jeff,

> I get a similar problem with the following complete program:
>
>    import Image
>    i = Image.fromstring("RGB", (1, 1), chr(255) * 3)
>    j = i.convert("P")
>    print j.getpixel((0,0))  # prints 252, should print 255
>
>This seems to have something to do with converting to a palette image.

You are right, the "problem" is is with the palette. The way we invoke convert 
it uses the default palette. The entry #252 = (252,252,252) is the entry 
closest to white that it contains (there are only 255 entries at all). 

The trick is to tell the convert method to use an adaptive palette, i. e. call
palette_im = im.convert("P", palette=Image.ADAPTIVE).
palette_im.getpixel((0,0)) will return #0 in your example, because there will 
be only one entry in the palette which is #0 = (255,255,255).

Thanks again Jeff, for pointing out it's a problem with the palette. And 
thanks to PIL people for their great product and access to the source, so the 
hidden APIs could be found....

Best regards,
  Henryk Gerlach

--
www.compals.com



More information about the Image-SIG mailing list