How to check for empty (mono-color) 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?".
Post Reply
graceman9
Posts: 5
Joined: 2011-03-17T23:20:07-07:00
Authentication code: 8675308

How to check for empty (mono-color) image?

Post by graceman9 »

subj.
For example, I cut the picture on the tiles, and I want to know which of them are empty? (transparent, or the same color). Thanks for the replies.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check for empty (mono-color) image?

Post by fmw42 »

get the standard-deviation from the verbose statistics for each tile. If the standard deviation is 0, then the image is one color. You may have to look channel by channel including the alpha channel.

Alternate is to use string formats. See http://www.imagemagick.org/script/escape.php


convert image -format "%[standard_deviation]" info:

returns values between 0 and QuantumRange (255 for 8-bit or 65535 for 16-bit IM compiles0

or

convert image -format "%[fx:u.standard_deviation]" info:

returns values between 0 and 1
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to check for empty (mono-color) image?

Post by anthony »

Number of colors %k will also do this! If you have one unique color then it will be 1
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
graceman9
Posts: 5
Joined: 2011-03-17T23:20:07-07:00
Authentication code: 8675308

Re: How to check for empty (mono-color) image?

Post by graceman9 »

fmw42 wrote:convert image -format "%[standard_deviation]" info:
Not work for me. I also try

Code: Select all

identify -format %[standard-deviation]
but two same (empty and transparent images, size 450 and 452 bytes) give different results:

Code: Select all

$ identify -format %[standard-deviation] 1.png
11571.2
$ identify -format %[standard-deviation] 2.png 
7529.32
MY TWO TRANSPARENT IMAGES BELOW:
Image
Image
I see no difference in eog or gimp, but when I upload image i saw this (white lines in top of image):
http://pic.lg.ua/s/g/0YCfF/DYPSF
anthony wrote:Number of colors %k will also do this! If you have one unique color then it will be 1
Yes, it is! :)

And new question: how can I diff "white mono-color image" from "gray mono-color image" ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to check for empty (mono-color) image?

Post by fmw42 »

see -compose minus or -compose different or compare

http://www.imagemagick.org/Usage/compose/#minus
http://www.imagemagick.org/Usage/compose/#difference

http://www.imagemagick.org/Usage/compare/

Can you be more specific about what type of compare you want -- what kind of result -- an image or a number
graceman9
Posts: 5
Joined: 2011-03-17T23:20:07-07:00
Authentication code: 8675308

Re: How to check for empty (mono-color) image?

Post by graceman9 »

Excuse me for the delay in replying.
I need the result - a number.
That's what I found:

Code: Select all

alpha=`convert $f -resize 1x1 txt: | sed '1d' | sed 's/\([^)]*\).*/\1/' | sed 's/,/ /g' | awk '{print $NF}'`
But I still want to do this without "sed" expression :) any advice?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to check for empty (mono-color) image?

Post by anthony »

graceman9 wrote:And new question: how can I diff "white mono-color image" from "gray mono-color image" ?
Just crop a single pixel from the image and output a TXT: image.

Code: Select all

  convert image.png  -crop 1x1+0+0 txt:
See TXT: image file format and Extracting Image Color

OR just get IM to print the images 0,0 color using -format and info:

Code: Select all

  convert image.png -format "%[pixel:s]" info:
See info: identify output and FX Expressions as Escapes

With one image you can also -print (only outputs its formatted string once, not once per image.

Code: Select all

  convert image.png  -print "%[pixel:s]" null:
See Formated Text Output Options
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply