Page 1 of 1

Best way to detect blank transparent image

Posted: 2018-03-05T12:52:39-07:00
by banisco
I want to determine the best/fastest way to detect if an image is entirely blank (assume image has alpha channel).

Test image https://www.dropbox.com/s/46zdazxrgy83g ... y.png?dl=0

Possible approaches:

1. Check for mean of zero (or standard deviation of zero?)
identify -format "%[opaque] %[fx:mean] %[fx:standard_deviation]" empty.png
False 0 0

Note: Photoshop reports mean of 255, so perhaps standard deviation is best value to go by?
Also, for my sample image, Imagemagick reports it as a Greyscale with Alpha, Photoshop treats it as RGB with Alpha - not sure why)

2. Check bounding box
identify -format "[%k]" emtpy.png
identify: geometry does not contain image `empty.png' @ warning/attribute.c/GetImageBoundingBox/240.
[0x0+855+540]

I like this approach since Imagemagick gives a nice warning message - any caveats to this technique?

Version: ImageMagick 7.0.7-14 Q16 x64 2017-12-06
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib jng jp2 jpeg lcms lqr openexr pangocairo png ps raw rsvg tiff webp xml zlib

Re: Best way to detect blank transparent image

Posted: 2018-03-05T14:19:08-07:00
by fmw42
identify -format "[%k]" emtpy.png
This is not a valid command. Properly it should be

Code: Select all

identify -format "%k" emtpy.png
But that tells you how many unique colors. If I do

Code: Select all

convert -size 100x100 xc:none tmp.png
convert tmp.png -format "%k" info:
1

It returns one unique color.

I would simply recommend

Code: Select all

convert image -channel a -separate -scale 1x1! -format "[fx:mean]\n" info:
If it is full transparency, you get 0 and fully opaque you get 1 and partially transparent is some value in-between.

Re: Best way to detect blank transparent image

Posted: 2018-03-05T15:31:15-07:00
by banisco
Apologies for the invalid command. I meant to use this for determining the bounding box:

Code: Select all

identify -format "%@" empty.png
Thanks for the recommended approach!

Re: Best way to detect blank transparent image

Posted: 2018-03-05T15:33:07-07:00
by fmw42
banisco wrote: 2018-03-05T15:31:15-07:00 Apologies for the invalid command. I meant to use this for determining the bounding box:

Code: Select all

identify -format "[%@]" empty.png
Thanks for the recommended approach!
That is not valid either. Leave off the brackets. Use brackets only when using fx:

Properly:

Code: Select all

identify -format "%@" empty.png
The virtual canvas would only be useful if you have a null transparent image with size 0x0. I do not see how that would help if you have an image that is transparent, but larger than 0x0. Are you looking for bad images?

Please clarify what you mean by "blank image". Is that just fully transparent?

Re: Best way to detect blank transparent image

Posted: 2019-02-12T09:03:46-07:00
by squiddy
fmw42 wrote: 2018-03-05T14:19:08-07:00 I would simply recommend

Code: Select all

convert image -channel a -separate -scale 1x1! -format "[fx:mean]\n" info:
If it is full transparency, you get 0 and fully opaque you get 1 and partially transparent is some value in-between.
Perfect ready-made answer to a problem I just tripped over today ... with the tiny exception that the command should be:

Code: Select all

convert image -channel a -separate -scale 1x1! -format "%[fx:mean]\n" info: 
:D

Re: Best way to detect blank transparent image

Posted: 2019-02-12T15:20:06-07:00
by fmw42
Yes, correct. That was a typo on my part. All string formats need to start with %