[Image-SIG] Re: colorshift while converting png to gif

Fredrik Lundh fredrik at pythonware.com
Wed Feb 9 19:08:52 CET 2005


Jeff Epler wrote:

> 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 really mean

    print j.convert("RGB").getpixel((0,0))  # prints (252, 252, 252), should print (255, 255, 255)

here -- but the fact that you did get index 252 was just what I needed to realize
what the problem was (the target palette contains a 6x6x6 color cube, but also a
couple of grayscale values, and the palette matching algorithm considers 252 to
be "close enough", despite the fact that the palette contains better matches).  the
attached patch fixes the problem.

</F>

===================================================================
--- libImaging/Palette.c        (revision 2287)
+++ libImaging/Palette.c        (working copy)
@@ -61,9 +62,14 @@
     if (!palette)
        return NULL;

+    for (i = 0; i < 10; i++) {
+       palette->palette[i*4+0] =
+       palette->palette[i*4+1] =
+       palette->palette[i*4+2] = 0;
+    }
+
     /* Simple 6x6x6 colour cube */

-    i = 10;
     for (b = 0; b < 256; b += 51)
        for (g = 0; g < 256; g += 51)
            for (r = 0; r < 256; r += 51) {
@@ -73,6 +79,12 @@
                i++;
            }

+    for (; i < 256; i++) {
+       palette->palette[i*4+0] =
+       palette->palette[i*4+1] =
+       palette->palette[i*4+2] = 0;
+    }
+
     return palette;
 }





More information about the Image-SIG mailing list