Best way to detect blank transparent 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
banisco
Posts: 10
Joined: 2016-08-12T15:31:12-07:00
Authentication code: 1151

Best way to detect blank transparent image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best way to detect blank transparent image

Post 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.
banisco
Posts: 10
Joined: 2016-08-12T15:31:12-07:00
Authentication code: 1151

Re: Best way to detect blank transparent image

Post 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!
Last edited by banisco on 2018-03-05T15:41:37-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best way to detect blank transparent image

Post 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?
squiddy
Posts: 16
Joined: 2018-09-27T02:53:52-07:00
Authentication code: 1152

Re: Best way to detect blank transparent image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best way to detect blank transparent image

Post by fmw42 »

Yes, correct. That was a typo on my part. All string formats need to start with %
Post Reply