Color count in PIL

L

Larry

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!
 
L

Larry

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.
 
D

David M. Cooke

At said:
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 ] )
 
F

Fredrik Lundh

Larry said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top