1

I'm trying to identify the total number of colors in an image. I am attempting this using identify and I am using version 6.9.10-68.

The manual says to use this format: identify [options] input-file and it also says that identifying the number of colors in an image is off by default and to enable it with the -define unique=true option. Further in the manual it says -define unique=true will "return the number of unique colors in the image" but it also gives the parameter -unique to "display the number of unique colors in the image".

I have been unsuccessfully been trying to use the following commands on my images:

identify -define unique=true input.jpg
identify -define "unique=true" input.jpg
identify -define unique=true -unique input.jpg
identify -define "unique=true" -unique input.jpg
identify -unique -define unique=true input.jpg
identify -unique -define "unique=true" input.jpg

I've also found other forum posts from 2008 mentioning -format %k and -format "%k" to which I've tried adding to the mix.

I've had some success with the -verbose parameter and using regex to get the Colors: \d+ value, but I've noticed sometimes it doesn't give a colors value and sometimes its 0 which doesn't make any sense to me.

I've tried converting the image to different formats but I haven't had much success with that either.

Is it possible to reliably and accurately get an integer for how many colors are in an image?

Ultimately my goal is to detect images that have only 1 color. I'm working with slippy maps and if I can flag a tile with only 1 color then I know all its "children" tiles will look the exact same and I can redirect requests to the same tile for a much faster user experience and for less data storage and transfer requirements.

GFL
  • 1,278
  • 2
  • 15
  • 24
  • 1
    The page for -unique-colors is at https://legacy.imagemagick.org/Usage/quantize/#extract. The command to get a list of colors is simply, `convert image -unique-colors -depth 8 txt:` I do not see any define for -unique-colors at https://imagemagick.org/script/defines.php. But when drawing a histogram, you need to do `-define histogram:unique-colors=true`. You can get the total number of unique colors from %k – fmw42 Apr 27 '21 at 00:33
  • Thank you very much. I wonder if the man page outdated or if I was just misunderstanding the tools. With convert, if I'm trying to get the number of colors rather than a list a colors would I just pipe it to wc -l to count the lines (and then substract 1 since convert provides an extra line before it lists colors)? And does the `-depth 8` parameter ensure that even if the image contains many colors it would cap the count at 8 (which is plenty for my needs)? – GFL Apr 27 '21 at 01:03
  • In tests I'm finding sometimes the number is -1 which means 0 lines were returned. Are there situations where a jpg could have 0 colors? I'm also getting numbers greater than 8 so my assumption about depth was wrong, but I'm not too concerned about that. – GFL Apr 27 '21 at 01:13
  • 1
    -depth 8 means 8-bit color values as opposed to 16-bit color values. To get the number of unique colors just use %k `convert image -format "%k\n" info:` e.g. `convert logo: -format "%k\n" info:` returns 256. Note that logo: is an internal imagemagick image. Replace that with your image.suffix type naming. – fmw42 Apr 27 '21 at 01:17
  • 1
    Post an example image that returns 0 for the number of unique colors and your exact command line. – fmw42 Apr 27 '21 at 01:17
  • 1
    `identify -unique image.suffix` should also work, but the result is buried within a bunch of other information. So %k is the best option. – fmw42 Apr 27 '21 at 01:41
  • It turns out "0" wasn't being returned, but "". When I was subtracting 1 from the line count I was getting -1. Now that I'm using `convert image -format "%k\n" info:` to get a number and not having to subract 1 I'm no longer having that issue. However, there are times when I'm getting no return from `convert image -format "%k\n" info:`. When I look at the images, so far, they seem to have many colors in them. Does it sometimes return nothing if there are a lot of colors? – GFL Apr 27 '21 at 13:11
  • 1
    How large is your image in width and height and file size? Can you post one? It is possible that you are running out of RAM or /tmp disk space. What image format is the file? – fmw42 Apr 27 '21 at 15:42

0 Answers0