Number of colors in an image

Laszlo Zsolt Nagy gandalf at geochemsource.com
Fri Nov 26 13:58:02 EST 2004


Hello Will,

Friday, November 26, 2004, 7:48:08 PM, you wrote:

> Laszlo Zsolt Nagy wrote:
>>   Hello,
>> 
>> 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?

> You may get a better response on how to do it in Python, but the 
> following C++ code should be easy to translate if necessary. All it 
> requires is a get pixel function.

> int CImage::CountUsedColours() const
....

> Regards,

> Will McGugan

Yes, this can be efficient in C++. I'm not sure but possible it is not
so efficient in Python. Also it would be far better to have direct
access to the pixels (and not using a method/function). Now I'm
thinking about ImageMagick and something like this:

        f = os.popen('identify -verbose ' + fullpath + ' | grep -e "Colors:"')
        s = f.read()
        index = s.find(':')
        color_count = long( (s[index+1:]).strip() )

Probably ImageMagick uses direct access to pixels but it still take
seconds to run. It is not that portable but I'll stay at this version.

:-)

Thank you very much.

-- 
Best regards,
 Laszlo

 mailto:gandalf at geochemsource.com
 web:http://designasign.biz
 




More information about the Python-list mailing list