Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Color count in PIL

Reply
Thread Tools

Color count in PIL

 
 
Larry
Guest
Posts: n/a
 
      01-27-2004
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!
 
Reply With Quote
 
 
 
 
Larry
Guest
Posts: n/a
 
      01-29-2004
(Larry) wrote in message news:<. 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.
 
Reply With Quote
 
 
 
 
David M. Cooke
Guest
Posts: n/a
 
      01-29-2004
At some point, (Larry) wrote:

> (Larry) wrote in message news:<. 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-30-2004
"Larry" <> 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>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL: problem to convert an image array to PIL format Sverre Python 2 12-17-2009 04:33 PM
Changing font color from current font color to black color Kamaljeet Saini Ruby 0 02-13-2009 04:58 PM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
[PIL] is there a downloadable docs for PIL Egor Bolonev Python 2 12-24-2004 11:05 AM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57