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
Best way to detect blank transparent image
- 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
This is not a valid command. Properly it should beidentify -format "[%k]" emtpy.png
Code: Select all
identify -format "%k" emtpy.png
Code: Select all
convert -size 100x100 xc:none tmp.png
convert tmp.png -format "%k" info:
It returns one unique color.
I would simply recommend
Code: Select all
convert image -channel a -separate -scale 1x1! -format "[fx:mean]\n" info:
Re: Best way to detect blank transparent image
Apologies for the invalid command. I meant to use this for determining the bounding box:
Thanks for the recommended approach!
Code: Select all
identify -format "%@" empty.png
Last edited by banisco on 2018-03-05T15:41:37-07:00, edited 1 time in total.
- 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
That is not valid either. Leave off the brackets. Use brackets only when using fx:banisco wrote: ↑2018-03-05T15:31:15-07:00 Apologies for the invalid command. I meant to use this for determining the bounding box:
Thanks for the recommended approach!Code: Select all
identify -format "[%@]" empty.png
Properly:
Code: Select all
identify -format "%@" empty.png
Please clarify what you mean by "blank image". Is that just fully transparent?
Re: Best way to detect blank transparent image
Perfect ready-made answer to a problem I just tripped over today ... with the tiny exception that the command should be:fmw42 wrote: ↑2018-03-05T14:19:08-07:00 I would simply recommend
If it is full transparency, you get 0 and fully opaque you get 1 and partially transparent is some value in-between.Code: Select all
convert image -channel a -separate -scale 1x1! -format "[fx:mean]\n" info:
Code: Select all
convert image -channel a -separate -scale 1x1! -format "%[fx:mean]\n" info:
- 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
Yes, correct. That was a typo on my part. All string formats need to start with %