How to use a palette?

Fredrik Lundh fredrik at pythonware.com
Fri Jun 18 13:10:01 EDT 2004


Antoon Pardon wrote:

> Is it possible to know how many colors are in a palette

get the histogram, and count the number of non-zero bins
(len(filter(None, im.histogram())) usually does the trick).

> and which ones?

quickest solution: resize the image to (256, 1), convert to RGB,
and use getpixel((i, 0)) to get the color for color index i.

    lut = im.resize((256, 1)).convert("RGB")
    print lut.getpixel((0, 0)) # get color #0

or use getdata() to get an array of color indices:

    lut = im.resize((256, 1)).convert("RGB").getdata()
    print lut[i]

</F>







More information about the Python-list mailing list