469,352 Members | 1,788 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Color count in PIL

I've been walking up, down, and around instances in the object model
of PIL trying to figure out how to easily calculate how many unique
colors are used in an image, specifically a bitmap (mode "P"). Am I
missing something?

Thanks python friends!
Jul 18 '05 #1
3 2399
wr**@email.com (Larry) wrote in message news:<2e**************************@posting.google. com>...
I've been walking up, down, and around instances in the object model
of PIL trying to figure out how to easily calculate how many unique
colors are used in an image, specifically a bitmap (mode "P"). Am I
missing something?

Thanks python friends!


So far I have: colors=len(sets.Set(list(im.getdata())))

That makes me want to throw up.
Jul 18 '05 #2
At some point, wr**@email.com (Larry) wrote:
wr**@email.com (Larry) wrote in message news:<2e**************************@posting.google. com>...
I've been walking up, down, and around instances in the object model
of PIL trying to figure out how to easily calculate how many unique
colors are used in an image, specifically a bitmap (mode "P"). Am I
missing something?

Thanks python friends!


So far I have: colors=len(sets.Set(list(im.getdata())))

That makes me want to throw up.


Probably very inefficient. Have a look at im.histogram(). Something
like:

colours = sum( [ 1 for h in im.histogram() if h != 0 ] )

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #3
"Larry" <wr**@email.com> wrote:
I've been walking up, down, and around instances in the object model
of PIL trying to figure out how to easily calculate how many unique
colors are used in an image, specifically a bitmap (mode "P"). Am I
missing something?


colors = len(filter(None, im.histogram()))

</F>


Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Gary Wessle | last post: by
2 posts views Thread by DrewKull | last post: by
22 posts views Thread by MP | last post: by
10 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
7 posts views Thread by Chris | last post: by
6 posts views Thread by Geetha | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.