Page 3 of 3

Re: Telling the difference between color and grayscale image

Posted: 2017-08-16T07:38:56-07:00
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:

Re: Telling the difference between color and grayscale image

Posted: 2017-08-16T09:21:37-07:00
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.