How to identify color in grayscale image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
bhanupriyagattu

How to identify color in grayscale image

Post by bhanupriyagattu »

i am having a problem that how to identify if any color presenting in grayscale image and here iam uisng image in cmyk mode
i don't know which command line tool i have to use
Kindly help me it is important to me
Thanks in Advance
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to identify color in grayscale image

Post by fmw42 »

If the image is truly only gray and no other near gray colors or any other colors, then each channel should have the same average value. There are several ways to test that, but I think the easiest is:

convert image -colorspace rgb -scale "1x1!" -format "%[pixel:s.p{0,0}]" info:

This will return a color name or an rgb triplet. If the color is not a gray shade or the rgb triplet values are not identical, then the image has some color.

The other way, is

convert image -colorspace rgb -verbose info:

then look at each of the RGB statistics and check the mean value. If the mean of each of the rgb values are not the same, then it has some color.

Or better:

test=`convert image -colorspace rgb -format "%[fx:u.mean.r==u.mean.g==u.mean.b?1:0]" info:`
[ $test -eq 1 ] && echo "gray" || echo "color"


This is unix. If on windows, see http://www.imagemagick.org/Usage/windows/
bhanupriyagattu

Re: How to identify color in grayscale image

Post by bhanupriyagattu »

Thanks for your reply
i got the answer
still i am having a doubt
i am having epsiamge.eps and in cmyk mode
now i want to check the colormode of this image
suppose the image is in grayscale mode here i have check any colors are there .....in that image
is it possible with this imagemagick
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to identify color in grayscale image

Post by fmw42 »

with vector images, you need to convert the image to RGB while reading it, so try

test=`convert -colorspace rgb image.eps -format "%[fx:u.mean.r==u.mean.g==u.mean.b?1:0]" info:`
[ $test -eq 1 ] && echo "gray" || echo "color

I am having trouble with eps images due to a fault in my GS, I believe so I cannot test with that kind of image. But the above concept works on raster images, such a png, jpg, tif, etc
Post Reply