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?".
dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Telling the difference between color and grayscale image

Post by dargaud » 2010-05-11T14:23:30-07:00

Hello all,
I read about this recently in some IM FAQ, but tonight my google-fu must be out for a drink because I can't find the relevant info.
I would like a simple way to tell if an image is grayscale or color.
Bonus points if it can tell the difference between 2-color B&W, grayscale, sepia, comic book (N-colors) or color...
I can see several ways to do it, each probably impractical for some reason:
- sum of Saturation value of each pixel / nb of pixels
- average intensity of the difference between the image and its desaturated version
- ...
I only know how to do very basic things in IM, so the above are already a bit out of my reach.
Suggestions ?

snibgo
Posts: 9403
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Telling the difference between color and grayscale image

Post by snibgo » 2010-05-11T14:42:31-07:00

"identify -verbose" under "Type" will have "Grayscale" if it is grayscale.

You can count the number of unique colours with "-unique-colours". I don't know how you would define "sepia". Perhaps convert to HSL space and find the spread of hues.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Telling the difference between color and grayscale image

Post by fmw42 » 2010-05-11T15:29:40-07:00

convert logo: -format "%[colorspace]" info:
RGB

convert logo: -type grayscale -format "%[colorspace]" info:
Gray


convert logo: -format "%[channels]" info:
rgb


convert logo: -type grayscale -format "%[channels]" info:
gray


convert logo: -format "%r" info:
PseudoClassRGB


convert logo: -type grayscale -format "%r" info:
PseudoClassGray


Unique colors:
convert logo: -format "%k" info:
232

User avatar
anthony
Posts: 8870
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Telling the difference between color and grayscale image

Post by anthony » 2010-05-11T17:16:10-07:00

This is part of sorting images into image types. Specially covered in IM examples, Image Comparing, Sorting Images by Type
http://www.imagemagick.org/Usage/compare/#type_general

This covers known methods for color, greyscale, black-and-white, cartoon-like, etc.

It is also closely related to next section on determining 'image metrics' which is a small amount of data that is used to describe an image so that similar (but not the same) image will have a similar metric.

Any input, feedback and contributions on the above is most welcome.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

User avatar
anthony
Posts: 8870
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Telling the difference between color and grayscale image

Post by anthony » 2010-05-11T17:22:45-07:00

Note, something of interst to you may be the test of 'linear color'
http://www.imagemagick.org/Usage/compare/#type_linear

That is an image may have color, but all the color falls on a line though color space.
I have used this test successfully to match up greyscale images that have been 'colored'.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Re: Telling the difference between color and grayscale image

Post by dargaud » 2010-05-12T03:08:54-07:00

Thanks guys,

'format' was not what I was looking for, I should have mentioned that all images are jpg.

The 'image sorting' usage info, now that's it.
Why not just do a convert -scale 1x1 and see if all 3 channels are identical ? Risk of false positive ?

The section about linear color seems to be exactly what I'm looking for (solves the 'sepia'), but it mentions a script and an email for more info, without specifying either. Any help on that ?

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Telling the difference between color and grayscale image

Post by fmw42 » 2010-05-12T08:59:06-07:00

'format' was not what I was looking for, I should have mentioned that all images are jpg.
The -format in the string commands has nothing to do with jpg. It has to do with returning string formats as described above, which give you just what you want regarding rgb vs grayscale. Grayscale is just telling you that all 3 rgb channels are the same.

Why not just do a convert -scale 1x1 and see if all 3 channels are identical ? Risk of false positive ?
that would certainly work but would be slower.

dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Re: Telling the difference between color and grayscale image

Post by dargaud » 2010-05-12T15:55:42-07:00

You are right, your convert logo: -format "%k" info: works very well in my case.
Thanks

User avatar
anthony
Posts: 8870
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Telling the difference between color and grayscale image

Post by anthony » 2010-05-12T21:49:12-07:00

dargaud wrote:I should have mentioned that all images are jpg.
JPEG images may never be pure-greyscale!!!! (can someone confirm this)

Basically JPEG is a 'lossy' file format in that it losses information about the image
that could cause some grey colors to become 'near-grey' colors.

As such your test must have some type of 'fuzziness' threshold in it greyscale test.
The ones I listed in IM examples, Compare, Image Types has that control.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/

dargaud
Posts: 31
Joined: 2006-08-06T01:58:58-07:00

Re: Telling the difference between color and grayscale image

Post by dargaud » 2010-05-14T14:24:49-07:00

JPEG images may never be pure-greyscale!!!!
Yes, that is true. I tested it on a couple thousand BW images from various sources. Obviously, single channel JPG images always have less than 256 colors. 3-channel JPGs from pure greyscale can have up to about a thousand colors in my tests. The reasons can be various and not only to alleged JPG artefaction which I cannot confirm. For instance images that have been resized by older versions of IrfanView have a colored right border for some resizing ratios. In my case some pics are sepia, from 'artistic' photography I took years ago: those show below the 4000 color range, even for large images. As for true color images, as long as they are big enough, they are way over ten times that.

snibgo
Posts: 9403
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Telling the difference between color and grayscale image

Post by snibgo » 2010-05-25T08:08:00-07:00

Grayscale is 8-bit while color is 24-bit.
But some colour images are 8 bits per pixel, eg images with a pallette.

And some greyscale images are 24 bits per pixel (one channel of 24 bits, or 3 equal channels of 8 bits).
snibgo's IM pages: im.snibgo.com

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 » 2017-08-08T06:59:20-07:00

did any one ever figure out a good way to separate Grayscale images vs color? i have gray scale images from patent software but they contain color pixel sometimes and i need to figure out a way to separate color images vs gray scale what has some color in it.

snibgo
Posts: 9403
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Telling the difference between color and grayscale image

Post by snibgo » 2017-08-08T08:13:48-07:00

A simple method is to convert to HSL or HCL and examine the G channel. If it is entirely black (ie the maximum value is zero), the image is grayscale. Otherwise, it is colour. (But beware of rounding errors.)

If IM reports an image as grayscale, then it contains only gray. Otherwise, it might still be grayscale. You can separate the RGB channels, then find the difference between R and G, and between G and B. If both differences are zero, the image contains only gray. Otherwise, it has some colour.
snibgo's IM pages: im.snibgo.com

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 » 2017-08-08T09:53:23-07:00

My Gray scale image has colors in it but i need to have some short of way to figure out it is gray scale image. For example i have gray scale patent of T-shirt but have image in middle that is color. Any idea ?>

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Telling the difference between color and grayscale image

Post by fmw42 » 2017-08-08T10:11:50-07:00

An image that is gray will have near zero saturation. So extract the saturation channel and measure its average value. If above some threshold near zero, then it has some color in it. The following will tell you the percent non-grayscale.

Code: Select all

convert image -colorspace HSB -channel green -separate +channel -format "%[fx:100*mean]" info:
or

Code: Select all

convert image -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]" info:

If you want a true/false test given a numerical percent threshold (replace threshold below with the value), then

Code: Select all

convert image -colorspace HSB -channel green -separate +channel -format "%[fx:100*mean>theshold?1:0]" info:
If returns 1, then it has color. If it returns 0, then it is grayscale.

Post Reply