How to find the number of unique colors in an 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?".
Post Reply
Evgeny
Posts: 3
Joined: 2010-03-13T13:23:33-07:00
Authentication code: 8675308

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

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

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

Post by magick »

Try this command:
  • identify -verbose -unique rose:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Evgeny
Posts: 3
Joined: 2010-03-13T13:23:33-07:00
Authentication code: 8675308

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

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

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

Post 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.
Evgeny
Posts: 3
Joined: 2010-03-13T13:23:33-07:00
Authentication code: 8675308

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

Post by Evgeny »

My version (6.5.7-8) does not have -unique option. It's good to know it was added.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
afeld
Posts: 1
Joined: 2016-11-15T22:24:32-07:00
Authentication code: 1151

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

Post by afeld »

Just discovered via Stack Overflow (http://stackoverflow.com/questions/1995 ... magemagick) that you can use

identify -format %k <filename>
Post Reply