Determine How Many Unique Colors in an Image

984 views Asked by At

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.

0

There are 0 answers