Telling the difference between color and grayscale image
Telling the difference between color and grayscale image
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 ?
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 ?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Telling the difference between color and grayscale image
"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.
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
- 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
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
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
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Telling the difference between color and grayscale image
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.
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
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Telling the difference between color and grayscale image
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'.
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
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Telling the difference between color and grayscale image
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 ?
'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 ?
- 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
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.'format' was not what I was looking for, I should have mentioned that all images are jpg.
that would certainly work but would be slower.Why not just do a convert -scale 1x1 and see if all 3 channels are identical ? Risk of false positive ?
Re: Telling the difference between color and grayscale image
You are right, your convert logo: -format "%k" info: works very well in my case.
Thanks
Thanks
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Telling the difference between color and grayscale image
JPEG images may never be pure-greyscale!!!! (can someone confirm this)dargaud wrote:I should have mentioned that all images are jpg.
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
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Telling the difference between color and grayscale image
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.JPEG images may never be pure-greyscale!!!!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Telling the difference between color and grayscale image
But some colour images are 8 bits per pixel, eg images with a pallette.Grayscale is 8-bit while color is 24-bit.
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
Re: Telling the difference between color and grayscale image
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Telling the difference between color and grayscale image
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.
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
Re: Telling the difference between color and grayscale image
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 ?>
- 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
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.
or
If you want a true/false test given a numerical percent threshold (replace threshold below with the value), then
If returns 1, then it has color. If it returns 0, then it is grayscale.
Code: Select all
convert image -colorspace HSB -channel green -separate +channel -format "%[fx:100*mean]" info:
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: