Telling the difference between color and grayscale 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?".
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: Telling the difference between color and grayscale image

Post by divptl »

First i want to thank you for all the help. So far what i was able to do with ImageMaick is great. Is it possible to use below command and somehow exclude pixle close to gray scale pixle when averaging for color. I have jpg images [img]c:\test.jpg[/img]

convert c:\images\test.jpg -colorspace HCL -channel green -separate +channel -format "%M %[fx:100*mean]\n" info:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Telling the difference between color and grayscale image

Post by fmw42 »

First I create a test image that is black with a small area of red in the middle.

Code: Select all

convert -size 500x500 xc:gray -size 100x100 xc:red -gravity center -compose over -composite gray_red.png
If you want to know the color, do the following:

Code: Select all

convert gray_red.png \
\( -clone 0 -colorspace HCL -channel green -separate -threshold 1%  \) \
-alpha off -compose copy_opacity -composite -scale 1x1! -alpha off \
-format "%[pixel:u.p{0,0}]\n" info:
red

If you want to know the percentage of the red, then do

Code: Select all

convert gray_red.png -colorspace HCL -channel green -separate +channel -threshold 1% -format "%[fx:100*mean]\n" info:
4

So this is 4% red and the rest is pure grayscale.

Note I have used 1% in the threshold, you can change that if you want. The larger the value, the more "color" will be considered gray.
Post Reply