Page 1 of 2

how to count unique colors in image ?

Posted: 2008-12-01T17:47:33-07:00
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!!

Re: how to count unique colors in image ?

Posted: 2008-12-01T19:24:25-07:00
by el_supremo
Use the identify command like this:

Code: Select all

identify -format %k filename
Pete

Re: how to count unique colors in image ?

Posted: 2008-12-01T19:25:19-07:00
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.

Re: how to count unique colors in image ?

Posted: 2008-12-01T19:40:35-07:00
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

Re: how to count unique colors in image ?

Posted: 2008-12-01T20:00:54-07:00
by fmw42
try

identify -format "%k" sunset.png

Re: how to count unique colors in image ?

Posted: 2008-12-01T20:06:33-07:00
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 ...

Re: how to count unique colors in image ?

Posted: 2008-12-01T20:13:56-07:00
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

Re: how to count unique colors in image ?

Posted: 2008-12-02T03:07:35-07:00
by boarders' paradise
ah ... wonderful !!
Thanks so much, guys, you saved my day ... :D

Re: how to count unique colors in image ?

Posted: 2018-03-30T21:46:55-07:00
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 ?

Re: how to count unique colors in image ?

Posted: 2018-03-30T22:25:06-07:00
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

Re: how to count unique colors in image ?

Posted: 2018-03-30T22:32:20-07:00
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 !

Re: how to count unique colors in image ?

Posted: 2018-03-30T22:39:04-07:00
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.

Re: how to count unique colors in image ?

Posted: 2018-03-30T23:24:02-07:00
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.

Re: how to count unique colors in image ?

Posted: 2018-03-30T23:27:47-07:00
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 !

Re: how to count unique colors in image ?

Posted: 2018-03-31T02:12:47-07:00
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