[Image-SIG] What colour model does the image use

Fredrik Lundh fredrik at pythonware.com
Thu Nov 8 22:11:18 CET 2007

export at hope.cz wrote:

> I use im.getpixel((x,y)) to find out the colour of a pixel.
> But how can I find out in which color model  the  the  return value is?
> For example  for png picture format
> im.getpixel((20,50))  gives the result 60.
> What does the value mean?

the im.mode attribute tells you what color model the image is using (see 
the documentation for details).

> Is it possible to find out the RGB model values?

assuming the mode is "P" (for 8-bit palette images), you can either 
convert the entire image to RGB and then pick out the values for the 
pixels you need

     image = im.convert("RGB")
     rgb = image.getpixel((x, y))

or crop out just the pixel you want and convert that one:

     rgb = im.crop((x, y, x+1, y+1)).convert("RGB").getpixel((0, 0))

or use the pixel value as an index into the palette array:

     p = im.getpalette()
     v = im.getpixel((x, y))
     rgb = p[v*3:v*3+3]


More information about the Image-SIG mailing list