how to count unique colors in image ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

how to count unique colors in image ?

Post by boarders' paradise »

Hi. Sorry, I'm a beginner ... :(
I looked everywhere, but couldn't find out what command line command I have to enter to let ImageMagick count the unique colors in an image and write the result to the command line ...

Any ideas? Thanks so much!!
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: how to count unique colors in image ?

Post by el_supremo »

Use the identify command like this:

Code: Select all

identify -format %k filename
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to count unique colors in image ?

Post by fmw42 »

boarders' paradise wrote:Hi. Sorry, I'm a beginner ... :(
I looked everywhere, but couldn't find out what command line command I have to enter to let ImageMagick count the unique colors in an image and write the result to the command line ...

Any ideas? Thanks so much!!

First most images will have many many colors. So unless you restrict your colors, you will be overloaded with colors. IM verbose info will list your colors if there are not too many (I believe 1024 or less). Otherwise you need to quantize the colors or reduce the colors using -depth or some other way. Once you have an image with a reasonable number of colors, you can extract that in a number of ways (including the -unique-colors command) all of which are explained at http://www.imagemagick.org/Usage/quantize/#extract

Pete's method above is certainly the simplest.
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: how to count unique colors in image ?

Post by boarders' paradise »

Thanks so much for helping me. I read your suggestions, but couldn't get it to work. I want to display the current colors, NOT reduce them and display the reduced number (funny concept, LOL ;) )

Let's say I have a file called "sunset.png"

I want to know how many (unique) colors are in the image.
I would expect something like:

Code: Select all

 input: identify -unique-colors sunset.png
output: 13889
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to count unique colors in image ?

Post by fmw42 »

try

identify -format "%k" sunset.png
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: how to count unique colors in image ?

Post by boarders' paradise »

thanks so much. Yes, that's exactly what I was looking for. I'm so happy, I'm glad you helped me.
Sorry, that I didn't understand it right away, but I had a look into the ImageMagick documentation and there it says:

identify -format

... where "format" is the image format type. So I thought I had to replace %k with the image format.

Where can I find this info (%k, etc.) ? I had been looking for this thoroughly ...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to count unique colors in image ?

Post by fmw42 »

see

http://www.imagemagick.org/script/escape.php

and all of Anthony's documentation at http://www.imagemagick.org/Usage/

and some of my links on the bottom my IM scripts home page at http://www.fmwconcepts.com/imagemagick/index.php

or on my IM tidbits homepage at http://www.fmwconcepts.com/imagemagick/ ... /index.php
boarders' paradise
Posts: 14
Joined: 2008-12-01T17:42:18-07:00

Re: how to count unique colors in image ?

Post by boarders' paradise »

ah ... wonderful !!
Thanks so much, guys, you saved my day ... :D
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: how to count unique colors in image ?

Post by Alexvb6 »

Hi,

An addition to this (old but useful) post, is the case where you want to know if an image have some transparency data, or not.
Basically, you have to extract the Alpha channel, and count the numbers of colors in it.

If the count is greater than 1, then your alpha channel is used (it contains transparency data).
If the count is 1, then it have not transparency at all :-) (or it is fully transparent... but that is useless in the case of the images I process : they are never fully-transparent. And moreover, I'm only interested in knowing if an Alpha mask is at stake, or not).

For example, today, I had a PNG file holding an Alpha channel. But I needed to know if some transparent pixels were effectively present on this Alpha channel, or not.

You can be in presence of several types of files, and you cannot assume that they have transparency or not based on their extension.
A Png image can have an Alpha layer, but with all pixels visible. The same goes for a MIFF image.

So I share with the community the code below, that allows to extract the alpha channel, and to count the number of colors in it.

Code: Select all

c:\SOURCE.png ^
-set colorspace sRGB -alpha extract ^
c:\ALPHA-channel-of-PNG.miff
identify -format "%k" c:\ALPHA-channel-of-PNG.miff

ALL-IN-ONE ALTERNATIVE :
It is possible to use the "-identify" operator in the same "convert" command. Please note that you will obtain the Number of colors directly + some basic information about the image. The number of colors is on line 2. I never succeded into obtaining ONLY the numbers of colors (line 2), and to get rid of the basic image information (line 1) : If someone have an idea.. thanks !

Using Windows Dos Command-line :

Code: Select all

convert ^
 c:\SOURCE.png ^
 -set colorspace sRGB -alpha extract ^
 -identify -format "%k" info:
It throws me these 2 lines, where I would only need the Second on (stating 243 colors) :

Code: Select all

c:\SOURCE.png PNG 300x300 300x300+0+0 8-bit sRGB 24198B 0.016u 0:00.014
243
Any clue to this ?
Last edited by Alexvb6 on 2018-03-30T22:32:49-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to count unique colors in image ?

Post by fmw42 »

try

Code: Select all

convert SOURCE.png -alpha extract -scale 1x1! -format "%[fx:mean]" info:
if the result is <1, then you have some transparency.

or

Code: Select all

convert SOURCE.png -alpha extract -scale 1x1! -format "%[fx:mean<1?1:0]" info:
if the result is 1, then you have transparency, if 0, then no transparency
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: how to count unique colors in image ?

Post by Alexvb6 »

Hi Fred,

I have thought to this solution first : reducing to 1px is a great mean to obtain the main color of an image.
But concerning the Alpha channel, I am not willing to obtain the main color, but be sure that the semi-transparent pixels are considered into the evaluation process. So I am doubtful about reducing the Alpha channel size to 1px ... Because if some semi-transparent pixels are present, how exactly would the [fx:mean] operator operate ? I was not able to really understand it from the Docs : http://www.imagemagick.org/script/quantize.php

...But testing your code gives me the good results.. that's stunning ! Based on your reputation, and despite the fact of not having really understood the [fx:mean] operator by myself, I think we can go for this (so elegant) solution !
Last edited by Alexvb6 on 2018-03-30T22:52:13-07:00, edited 2 times in total.
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: how to count unique colors in image ?

Post by Alexvb6 »

PS : For curiosity, I have tried to use "%[colors]" instead of "[%k]", and the command-line does not seems to like that :

Code: Select all

convert: unknown image property "%[colors]" @ warning/property.c/InterpretImageP
roperties/3934.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to count unique colors in image ?

Post by fmw42 »

To be fully opaque the fx:mean of the alpha channel would have to be 1 (in fx the range is 0 to 1), i.e, every pixel having an fx value of 1. So if any pixels are not 1 (fully opaque) that would bring the mean value lower than 1.
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: how to count unique colors in image ?

Post by Alexvb6 »

#Fred : Brilliant and concise explanation !
That was close to what my mind was about to think, but explained this way, it totally makes sense :)
A big "Thank You" to start your day !
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: how to count unique colors in image ?

Post by snibgo »

You can also use %[opaque] thus:

Code: Select all

f:\web\im>%IM%convert toes.png -format %[opaque] info:
true

f:\web\im>%IM%convert toes_holed.png -format %[opaque] info:
false
snibgo's IM pages: im.snibgo.com
Post Reply