Palette-mode PNG images

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Tue Nov 18 22:22:13 EST 2008


In message <gfoe0s$11u$1 at lust.ihug.co.nz>, I wrote:

>           data = string.join(map(chr, data), "")
>       TypeError: an integer is required

OK, I figured that out, the putpalette call wants a sequence of integers
being (R, G, B, R, G, B ...), not a sequence of sequences ((R, G, B), (R,
G, B)...).

>     TheImage = TheImage.convert("P")

This gives me 8 bits per pixel, which is too many. And this automatically
converts the image using a default palette; a subsequent putpalette call
replaces the palette, but doesn't change the pixels, so the colours come
out wrong.

I can also do Image.convert("1"), which gives me a 1-bit-per-pixel image,
but that only allows a fixed colour table (black and white), no option to
change either entry.

> Also I see that the PIL PNG encoder/decoder supports a "bits" output
> option ...

Hmm, interesting, but adding either "bits = 2" or "bits = 1" to the save
call just seems to turn the image all black. Examination with
ImageMagick's "identify" command shows that the default colour table ramp
has been stair-stepped, with all components turned into multiples of 51 (=
255 / 5).




More information about the Python-list mailing list