Search code examples
imageimagemagickgraphicsmagick

Detecting mostly empty images using imagemagick


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/


Solution

  • 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