Page 1 of 1

How to find the number of unique colors in an image?

Posted: 2011-01-31T12:12:21-07:00
by Evgeny
The web page for identify says:
The information returned includes the image number, ... the number of colors in the image...
But the first example on that page does not seem to show the number of colors:
$magick> identify rose.jpg
rose.jpg JPEG 640x480 DirectClass 87kb 0.050u 0:01
I don't see the number of colors in the following example with the -verbose option either (it prints image depth but not the number of actually used colors).

The man page for identify says that printing the number of colors in the image is off by default and says to use "-define unique=true" option. When I do "identify -define unique=true image.tiff", I get
image.tiff TIFF 5096x6397 5096x6397+0+0 8-bit DirectClass 5.638MiB 0.000u 0:00.000
Am I missing something? Is it a bug that should be reported? How do I get the number of unique colors actually used and, if possible, their RGB values?
$ convert -version
Version: ImageMagick 6.5.7-8 2010-12-02 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
Features: OpenMP

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T12:18:09-07:00
by magick
Try this command:
  • identify -verbose -unique rose:

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T12:18:27-07:00
by fmw42
I believe the histogram is what is being referred to or the color table.

But see http://www.imagemagick.org/Usage/quantize/#extract and the section on -unique-colors

convert tree.gif -unique-colors -depth 16 txt:-

or change to -depth 8 if you are on Q16 IM and want 8-bit color values.

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T13:16:51-07:00
by Evgeny
Thanks for the prompt response.
But see http://www.imagemagick.org/Usage/quantize/#extract and the section on -unique-colors
Yes, this does indeed print the histogram. It also says that -verbose option does not print the histogram if the number of colors is > 1024. I wish, though, it would print some warning about this and also the number of colors.

I also discovered that "identify -define unique=true tree.gif" (tree.gif is an image from the link above) prints "8c", and "identify -verbose tree.gif" prints a colormap with 8 entries (the histogram has 6 entries). In contrast, I have a TIFF file with < 30 colors; "identify -verbose" does not print a colormap, only a histogram, and "identify -define unique=true" does not print the number of colors.

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T13:58:23-07:00
by magick
Recent versions of ImageMagick displays the unique number of colors in an image with 'identify -verbose -unique...' Perhaps older versions of ImageMagick does not support this option. A histogram is shown if the number of unique colors is less than or equal to 1024 colors. For a complete histogram, use the -unique-colors option that Fred suggests.

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T14:39:59-07:00
by Evgeny
My version (6.5.7-8) does not have -unique option. It's good to know it was added.

Re: How to find the number of unique colors in an image?

Posted: 2011-01-31T14:55:02-07:00
by fmw42
try -unique-colors not -unique

You can also generate a histogram for any image:

convert image -format "%c" histogram:info:

http://www.imagemagick.org/Usage/files/#histogram


Perhaps you could post a link to your image, so others can try using it to get your unique colors or histogram

Re: How to find the number of unique colors in an image?

Posted: 2016-11-15T22:28:11-07:00
by afeld
Just discovered via Stack Overflow (http://stackoverflow.com/questions/1995 ... magemagick) that you can use

identify -format %k <filename>