How recognize if a cmyk or rgb image is black and white

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?".
Post Reply
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

Hi all,
I'm trying to detect if a cmyk or rgb image is Black and White. According your opinion It's possible? In which way?
Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How recognize if a cmyk or rgb image is black and white

Post by fmw42 »

Do you mean binary (full white and full black only) or grayscale?

identify -verbose yourimage

look at each color channel statistics. If RGB and type=grayscale, then grayscale. If RGB and type=bilevel, then b/w only.

Don't know about type=grayscale or bilevel for CMYK, so you may have to convert your CMYK to RGB (-colorspace RGB) before checking the type.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How recognize if a cmyk or rgb image is black and white

Post by anthony »

See IM Examples, Comparing Images, Sorting by Type
http://www.imagemagick.org/Usage/compare/#type_general

Note there are big differences between the types..
  • pure Black and White 'bitmap' type images (as in old computer displays)
  • Mostly Black and White (such as sketches, text, and line drawings)
  • and grayscale (such as old Black and White photos, TV, or movies).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

Re: How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

I explained myself badly. I have many images that have CMYK and RGB color space, but they are black and white. How do I distinguish them? With the methods well described on the page of Anthony, the method to compare the photo with the clone in grayscale and calculate the maximum different colors works well but not always. For example there are photos in color compared to their clone greyscale have 5860 as a maximum which is often less than the maximum of a black and white photos. How to tell the difference?
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

Re: How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

http://www.freeimagehosting.net/1a5ca
http://www.freeimagehosting.net/55c55
Look at these two photos. The maximum (convert image.jpg \ (+ clone-colorspace gray \)-compose-difference-composite-colorspace gray-format '% [maximum]' info)
the first photo is 3009, the second is 2170. But the first is black and white (on the web is seen with a blue tint is because is CMYK if you open it with Photoshop is black and white), the second is colored. i would like to distinguish these two.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How recognize if a cmyk or rgb image is black and white

Post by fmw42 »

Neither image is black/white or grayscale. There is color in them. As I said before, look at the channel mean values and see how close they are to being the same at least for RGB. For CMYK convert to RGB first and then do the same.


convert 55c55.jpg -format "%[fx:mean.r]" info:
0.653639
convert 55c55.jpg -format "%[fx:mean.g]" info:
0.653254
convert 55c55.jpg -format "%[fx:mean.b]" info:
0.654195

Probably a more reliable measure would be to do a compare difference between channels and see how close to zero they are

convert 55c55.jpg -channel rg -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.000447883
convert 55c55.jpg -channel gb -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.00726641
convert 55c55.jpg -channel br -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.00745553
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

Re: How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

Very interesting Fmw42. But It's possible make an unique instruction instead of three "convert" command?
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

Re: How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

I obtain for an image
convert image.jpg -channel rg -separate +channel -compose difference -composite -format "%[fx:mean]" info:
6.55076e-05
convert image.jpg -channel gb -separate +channel -compose difference -composite -format "%[fx:mean]" info:
5.40435e-05
convert image.jpg -channel br -separate +channel -compose difference -composite -format "%[fx:mean]" info:
5.5787e-05

What it means ???
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How recognize if a cmyk or rgb image is black and white

Post by fmw42 »

It means that the results are nearly 0. That is scientific notation for 0.00005 in the range 0 to 1. So this image should have very little color in it. Zero for all three means a perfectly grayscale image, no color whatsoever. You have about 0.005% color, which is very minimal.

Another test would be to check the saturation. If close to zero, then it is nearly grayscale.



convert 55c55.jpg -colorspace hsb -channel g -separate +channel -format "%[fx:mean]" info:
0.0131665


So this implies 1.3% color or 1.3% deviation from pure grayscale
Paolo_Italia
Posts: 6
Joined: 2011-12-14T05:33:29-07:00
Authentication code: 8675308

Re: How recognize if a cmyk or rgb image is black and white

Post by Paolo_Italia »

http://www.freeimagehosting.net/81b4b
http://www.freeimagehosting.net/03ca3
I am in great confusion. Look at these two photos, the color has values ​​closer to zero than the black and white.
/usr/local/bin/convert -quiet image.jpg -colorspace hsb -channel r -separate +channel -format "%[fx:mean]" info:
0.0953648 ( the shoe ) for "our eyes" is a color picture
0.440809 ( the man ) for "our eyes" is a black white picture
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How recognize if a cmyk or rgb image is black and white

Post by fmw42 »

Paolo_Italia wrote:http://www.freeimagehosting.net/81b4b
http://www.freeimagehosting.net/03ca3
I am in great confusion. Look at these two photos, the color has values ​​closer to zero than the black and white.
/usr/local/bin/convert -quiet image.jpg -colorspace hsb -channel r -separate +channel -format "%[fx:mean]" info:
0.0953648 ( the shoe ) for "our eyes" is a color picture
0.440809 ( the man ) for "our eyes" is a black white picture

Very strange. Apparently, IM codes near black as saturation mid gray rather than zero saturation. I think this is because black can be any color with zero saturation, so the rgb <-> hsb transformation is indeterminant. Thus this technique is not going to work.

You can try something like this to get around it, but this is not really very good.


convert 03ca3.jpg -fuzz 10% -fill "rgb(1,1,1)" -opaque black -colorspace hsb -channel r -separate +channel -write 03ca3_tmp.png -format "%[fx:mean]" info:

I would go back to the difference concept:

convert 03ca3.jpg -channel rg -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.00311191

convert 03ca3.jpg -channel gb -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.00561559

convert 03ca3.jpg -channel br -separate +channel -compose difference -composite -format "%[fx:mean]" info:
0.00818051
Post Reply