PIL image.getcolors

Robin Becker robin at reportlab.com
Thu Nov 29 12:26:48 CET 2007


I have an RGBA .png which PIL is able to read and I would like to convert the 
image to a simple RGB image with a single colour reserved for transparent as the 
A channel is either all on or all off.

I thought I could use the the im.getcolors method to find the colours used by 
the image, but it sometimes returns None. Is there a smart way to find out an 
unused colour or the least used colour in my image?

Additionally can PIL change a specified colour quickly. I'm guessing I might be 
able to do it using the composite mechanism

> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import Image
>>>> im0 = Image.open('trans.png')
>>>> im0.mode
> 'RGBA'
>>>> im0.getcolors()
> [(55367, (255, 1, 25, 255)), (24104, (24, 79, 235, 255)), (46529, (0, 0, 0, 0))]
>>>> im1 = Image.open('400px-Wiki_letter_w.svg.png')
>>>> im1.mode
> 'RGBA'
>>>> im1.getcolors()
>>>> im1.getcolors() is None
> True
>>>>


-- 
Robin Becker




More information about the Python-list mailing list