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?".
snibgo
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

Post by snibgo »

divptl wrote: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.
That doesn't make sense. How can a grayscale image have colours?

Please show an example image. Also say what version IM you use, on what platform.
snibgo's IM pages: im.snibgo.com
User avatar
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

Post by fmw42 »

I suspect he means that his image is mostly gray shades, but contains a little color. But I would agree, it would be nice to see an example.
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 »

Sorry but i can't share images as it's company's patent . Basically i have patent of say shirt mostly in black and white but say in the pocket of it there is rose picture from IM sample. I need to separate this image from actual normal color picture.
User avatar
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

Post by fmw42 »

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 »

Thank you.

Any way to tell if its graphic vs picture ?
User avatar
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

Post by fmw42 »

Picture usually has many more colors. Graphic often has only a few. The only way of getting such information is from the histogram or from the number of unique colors.

This will give you a list of colors:

Code: Select all

convert logo: -depth 8 -unique-colors txt:
This will give you the number of colors (unix syntax)

Code: Select all

convert logo: -depth 8 -unique-colors txt:- | tail -n +2 | wc -l
256

However, there is no guarantee that you would not have a picture with only a few colors or a graphic with lots of colors.
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 »

if i want to check for gray scale in border (all 4 sides of the image) of the image is it possible ?
User avatar
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

Post by fmw42 »

crop the border edges one at a time and check with the saturation as described earlier.
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 »

Is there a way to do this same logic for Border only ? I am sorry for being pain but there are way too many fallout so i am thinking to go based on border of picture.
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 »

Sorry i didnot see that message . How do i corp and check for the colors at the same time . say 2% of the picture is my border.
User avatar
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

Post by fmw42 »

Read my post above: It says, crop the border edges one at a time and check with the saturation as described earlier. For example:

Code: Select all

convert image -gravity north -crop 100x10% +repage -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
convert image -gravity east -crop 10x100% +repage -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
convert image -gravity south -crop 100x10% +repage -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
convert image -gravity west -crop 110x100% +repage -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
That will test each border of 10% thickness.
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 »

fmw42 wrote: 2017-08-08T15:51:17-07:00 Picture usually has many more colors. Graphic often has only a few. The only way of getting such information is from the histogram or from the number of unique colors.

This will give you a list of colors:

Code: Select all

convert logo: -depth 8 -unique-colors txt:
This will give you the number of colors (unix syntax)

Code: Select all

convert logo: -depth 8 -unique-colors txt:- | tail -n +2 | wc -l
256

However, there is no guarantee that you would not have a picture with only a few colors or a graphic with lots of colors.
what is the syntax for windows for "

Code: Select all

convert logo: -depth 8 -unique-colors txt:- | tail -n +2 | wc -l
"
snibgo
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

Post by snibgo »

I would use this instead:

Code: Select all

convert logo: -depth 8 -unique-colors -format %w info:
or:

Code: Select all

convert logo: -depth 8 -format %k info:
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 »

how do i do this for all image in DIR and Move the file if it's > threshold ?
snibgo
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

Post by snibgo »

You would write a script.
snibgo's IM pages: im.snibgo.com
Post Reply