[Image-SIG] using getcolors( ) properly

Fredrik Lundh fredrik at pythonware.com
Mon Apr 27 13:44:31 CEST 2009

On Sat, Apr 25, 2009 at 3:00 PM, Eduardo Ismael <eismb at hotmail.com> wrote:

> I had no success using im.getcolors(). I now there is something you have to
> do to avoid getting None as a result, but I could not understand the
> following from the manual:
> "To make sure you get all colors in an image, you can pass in
> size[0]*size[1]"
> Can anyone give an example of passing in "size[0]*size[1]"?

"size" refers to the size attribute of the image object.

to prevent unexpected memory/cpu blowup, getcolors() expects you to
specify the maximum number of colors you want.  the default is 256
colors (8 bits); the worst case is obviously the number of pixels in
the image, which you can get by multiplying the width by the height.

    im = .... some image ...
    size = im.size
    colors = im.getcolors(size[0]*size[1])


More information about the Image-SIG mailing list