How to check image color or back 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?".
vanquang
Posts: 5
Joined: 2011-10-04T01:11:44-07:00
Authentication code: 8675308

How to check image color or back and white

Post by vanquang »

Hi
i need check image color (don't accept back and white)
how to check it?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

vanquang wrote:Hi
i need check image color (don't accept back and white)
how to check it?

Please clarify? Do you mean you don't want any image that is totally black or totally white? Or do you mean you don't want any image that has even 1 black or 1 white pixel?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to check image color or black and white

Post by anthony »

For methods of sorting images into categories see...
Image Comparing, Sorting Images by Type
http://www.imagemagick.org/Usage/compare/#type_general
Whcih is also closely related to generating Image Metrics, for sorting before comparisons or database storage
http://www.imagemagick.org/Usage/compare/#metrics

What about line drawings or text? These are typically not truely black and white but mostly black and white with grey anti-aliasing pixels.For example...
Image
Or even... mostly two color gradient images (red on white with anti-aliasing) such as this.
Image
or images with large expanses of pure black or white, or a specific color?
Image
or just a small segment of pure color -- this rose image as an area of pure white!
Image
Also what about greyscale images?
Image
Or images with little 'energy' in the form of edges, only smooth color gradients
Image
What about transparency?
Image
Or even animations (simple or of real images)
Image Image

Basically you need to define exactly what you want to include or exclude!

Then we can work out something that can detect such images.

While we are on this topic, does anyone have any other basic 'types' of images?

Note I have ignored 'real world photos' which typically do not fit in the above basic groups, but has large numbers of image sub-categories in and of itself (faces, groups, seascapes, buildings, etc)!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
vanquang
Posts: 5
Joined: 2011-10-04T01:11:44-07:00
Authentication code: 8675308

Re: How to check image color or back and white

Post by vanquang »

fmw42 wrote:
vanquang wrote:Hi
i need check image color (don't accept back and white)
how to check it?

Please clarify? Do you mean you don't want any image that is totally black or totally white? Or do you mean you don't want any image that has even 1 black or 1 white pixel?
when i use ImageMagick check an image (not compare image)
if check image:
Image
return true
if check Image then return false
how to implement it?
vanquang
Posts: 5
Joined: 2011-10-04T01:11:44-07:00
Authentication code: 8675308

Re: How to check image color or black and white

Post by vanquang »

anthony wrote:For methods of sorting images into categories see...
Image Comparing, Sorting Images by Type
http://www.imagemagick.org/Usage/compare/#type_general
Whcih is also closely related to generating Image Metrics, for sorting before comparisons or database storage
http://www.imagemagick.org/Usage/compare/#metrics

What about line drawings or text? These are typically not truely black and white but mostly black and white with grey anti-aliasing pixels.For example...
Image
Or even... mostly two color gradient images (red on white with anti-aliasing) such as this.
Image
or images with large expanses of pure black or white, or a specific color?
Image
or just a small segment of pure color -- this rose image as an area of pure white!
Image
Also what about greyscale images?
Image
Or images with little 'energy' in the form of edges, only smooth color gradients
Image
What about transparency?
Image
Or even animations (simple or of real images)
Image Image

Basically you need to define exactly what you want to include or exclude!

Then we can work out something that can detect such images.

While we are on this topic, does anyone have any other basic 'types' of images?

Note I have ignored 'real world photos' which typically do not fit in the above basic groups, but has large numbers of image sub-categories in and of itself (faces, groups, seascapes, buildings, etc)!
when use "-format", "%k", i get an number (eg:2064) is mean?
i don't know it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to check image color or back and white

Post by anthony »

vanquang wrote:if check image:
Image
return true
if check Image then return false
how to implement it?
See... Compare, Sorting Images by Type, Gray-scale vs Color
http://www.imagemagick.org/Usage/compar ... _greyscale

Basically you convert the image to HSL and then output verbose information ("identify" statistics).
In HSL space Saturation define how colorful pixels are, and this is returned in the 'Green' channel statistics.

For example...

Code: Select all

  convert rose_grey.gif  -colorspace HSL -verbose info: 

Code: Select all

...
    Green:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
...
Max is 0 so no color was present at all! -- It is a perfect grayscale (or black and white) image


The original color rose image on the other hand generated...

Code: Select all

    Green:
      min: 2 (0.00636301)
      max: 255 (1)
      mean: 101.764 (0.399074)
      standard deviation: 71.0352 (0.278569)
      kurtosis: -1.0037
      skewness: 0.44952
Showing there was at least one pure color pixel (max = 100%) -- not pure grayscale
and that the image was very colorful in general too (mean is 39.9%) -- not even mostly grayscale (white background)!
Other statistics show how the color ratios was distributed about that mean.


Another method is to compare a grayscale version of the image with the original input image and see how different they are. This will let you find what pixels (areas) in the input image are colorful and by how much!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
vanquang
Posts: 5
Joined: 2011-10-04T01:11:44-07:00
Authentication code: 8675308

Re: How to check image color or back and white

Post by vanquang »

anthony wrote:
vanquang wrote:if check image:
Image
return true
if check Image then return false
how to implement it?
See... Compare, Sorting Images by Type, Gray-scale vs Color
http://www.imagemagick.org/Usage/compar ... _greyscale

Basically you convert the image to HSL and then output verbose information ("identify" statistics).
In HSL space Saturation define how colorful pixels are, and this is returned in the 'Green' channel statistics.

For example...

Code: Select all

  convert rose_grey.gif  -colorspace HSL -verbose info: 

Code: Select all

...
    Green:
      min: 0 (0)
      max: 0 (0)
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
...
Max is 0 so no color was present at all! -- It is a perfect grayscale (or black and white) image


The original color rose image on the other hand generated...

Code: Select all

    Green:
      min: 2 (0.00636301)
      max: 255 (1)
      mean: 101.764 (0.399074)
      standard deviation: 71.0352 (0.278569)
      kurtosis: -1.0037
      skewness: 0.44952
Showing there was at least one pure color pixel (max = 100%) -- not pure grayscale
and that the image was very colorful in general too (mean is 39.9%) -- not even mostly grayscale (white background)!
Other statistics show how the color ratios was distributed about that mean.


Another method is to compare a grayscale version of the image with the original input image and see how different they are. This will let you find what pixels (areas) in the input image are colorful and by how much!
when use "-format", "%k", i get an number (eg:2064) is mean?
i don't know it.

because i use in vbscript, please send
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

You are getting the number of shades of gray as 2064 from %k (unique colors or shades of gray)

try

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

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

This only works if your image is pure grayscale. Otherwise, you need to do as Anthony suggests and use the saturation image and threshold to test if low enough saturation that you would consider it gray.
vanquang
Posts: 5
Joined: 2011-10-04T01:11:44-07:00
Authentication code: 8675308

Re: How to check image color or back and white

Post by vanquang »

in vb script:

Code: Select all

Set img = CreateObject("ImageMagickObject.MagickImage.1")
reponse.write img.convert(fullpath,"-colorspace","HSL","-verbose")
return (w,h,type) only:

Code: Select all

57,200,JPEG
not return:
Green:
min: 0 (0)
max: 0 (0)
mean: 0 (0)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

try adding -format
wvanoeveren
Posts: 1
Joined: 2012-02-22T06:30:24-07:00
Authentication code: 8675308

Re: How to check image color or back and white

Post by wvanoeveren »

hello,

I guess this should be right post where i should put my question since i'm currently in search of a good method on how to determine if an image is more color than B/W.
I would like to use some kind of threshold but based on the different properties of the image (size, dpi, width, height, ...) so that i can determine whether i should take the color or the B/W image.
Is it possible to give me 1 (or more) command lines which i can use (returning their info) so that i can use it towards the threshold settings ?

Thanks in advance !
Wim
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

try testing the mean (average) saturation of the image.

convert image -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:


When the image is perfectly grayscale, the saturation result will be zero. The larger the resulting number (in range 0 to 1) the more color is involved. So you can decide on some threshold value for your decision about how much color to allow while still be nearly grayscale.


You can test this by using -modulate to adjust the saturation for an image. See http://www.imagemagick.org/script/comma ... p#modulate and http://www.imagemagick.org/Usage/color_mods/#modulate


convert logo: -modulate 100,X,100 -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:

when X=0, you get totally grayscale and the result is 0
when you increase X towards 100, you get a larger result as more color is allowed in the image.
JulietLindl
Posts: 3
Joined: 2014-10-03T13:38:58-07:00
Authentication code: 6789

Re: How to check image color or back and white

Post by JulietLindl »

My images keep coming out in black and white with ImageMagick? I'll try the suggestions here and report back my results. Thanks
Juliet
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

JulietLindl wrote:My images keep coming out in black and white with ImageMagick? I'll try the suggestions here and report back my results. Thanks
Please clarify. It is not clear what your issue is. You seem to have added a new post to an old topic, which may or may not be relevant to the old topic. In the future, please create a new post even if similar to an old one. And always provide your IM version and platform.

Please read viewtopic.php?f=1&t=9620, which is the top link on the User's forum.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check image color or back and white

Post by fmw42 »

fmw42 wrote:try testing the mean (average) saturation of the image.

convert image -colorspace HSL -channel g -separate +channel -format "%[fx:mean]" info:

Due to HSL being a double hexcone model, this colorspace does not work properly for measuring saturation for L>50% as it approaches the top peak. There white is going to be indetermined saturation and turns out is interpreted as high saturation. In its place, use HCL, HCLp, HSI or HSB above. These are all single hexcone type models and should work fine in the above equation.

Reference: https://en.wikipedia.org/wiki/HSL_and_HSV
Post Reply