Detecting mostly empty images using imagemagick

2.5k Views Asked by At

I'd like to use imagemagick or graphicsmagick to detect whether an image has basically no content.

Here is an example:

example

https://s3-us-west-2.amazonaws.com/idelog/token_page_images/120c6af0-73eb-11e4-9483-4d4827589112_embed.png

I've scoured Fred's imagemagick scripts, but I can't figure out if there is a way to do this:

http://www.fmwconcepts.com/imagemagick/

2

There are 2 best solutions below

1
On BEST ANSWER

Easiest way would be to use -edge detection followed by histogram: & text:. This will generate a large list of pixel information that can be passed to another process for evaluation.

convert 120c6af0-73eb-11e4-9483-4d4827589112_embed.png \
        -edge 1 histogram:text:- | cut -d ' ' -f 4 | sort | uniq -c

The above example will generate a nice report of:

50999 #000000
  201 #FFFFFF

As the count of white pixels is less then 1% of black pixels, I can say the image is empty.

This can probably be simplified by passing -fx information to utility.

convert 120c6af0-73eb-11e4-9483-4d4827589112_embed.png \
        -format '%[mean] %[max]' info:- | awk '{print $1/$2}'
#=> 0.00684814
0
On

If you are talking about the amount of opaque pixels vs the amount of transparent pixels, then the following will tell you the percentage of opaque pixels.

convert test.png -alpha extract -format "%[fx:100*mean]\n" info:

39.0626

Or if you want the percentage of transparent pixels, use

convert test.png -alpha extract -format "%[fx:100*(1-mean)]\n" info:

60.9374