Number of colors in an image

Fredrik Lundh fredrik at pythonware.com
Sat Nov 27 02:25:43 EST 2004


Laszlo Zsolt Nagy wrote:

> How can I determine the number of colors used in an image? I tried to
> search on Google but I could figure out. I read the PIL handbook but I
> do not see how to do it. Can anyone help?

PIL 1.1.5 has a "getcolors" method:

    http://effbot.org/zone/pil-image.htm#image-getcolors-method

    im.getcolors() => a list of (count, color) tuples or None

    im.getcolors(maxcolors) => a list of (count, color) tuples or None

    (New in 1.1.5) Returns an unsorted list of (count, color) tuples, where the
    count is the number of times the corresponding color occurs in the image.

    If the maxcolors value is exceeded, the method stops counting and returns
    None. The default maxcolors value is 256. To make sure you get all colors
    in an image, you can pass in size[0]*size[1] (but make sure you have lots
    of memory before you do that on huge images).

</F> 






More information about the Python-list mailing list