Fred's ImageMagick Scripts


    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at:


Computes the normalized entropy of an image channel-by-channel

Download Script

last modified: April 25, 2015

USAGE: entropy infile
USAGE: entropy [-h or -help]

-h or -help .... Displays help information

PURPOSE: To compute the normalized entropy of an image channel-by-channel.

DESCRIPTION: ENTROPY computes the normalized entropy of an image channel-by-channel. Entropy is a measure of graylevel distribution (disorder or randomness) in the histogram. It is not a measure of spatial disorder or spatial randomness in the image. The entropy is computed from the histogram of the channel by accumulating -p*ln(p) over every 8-bit graylevel, where p = (count for a given graylevel) / (total pixels). The normalized entropy is equal to the entropy/ln(256). For example, if the image is a single grayscale, then the entropy=0. If the image is a uniform gradient including all values from 0 to 255 that are equally populated in the histogram, then the entropy=1.

When the colorspace is Gray, only one Gray entropy value will be printed.

When the colorspace is CMYK, the following 5 values will be printed: Cyan, Magenta, Yellow and Black Entropies and the Average of those four entropies.

When the colorspace is RGB or any other colorspace, then the following 4 values will be printed to the terminal: Red, Green and Blue Entropies and the Average of those three entropies. For any colorspace other than RGB, then Red, Green and Blue should then be interpreted as channels 1, 2 and 3, respectively, for that colorspace.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


Image And Spectrum

Original Image


Gray Entropy = 0.125

Gray Entropy = 0.873

Red Entropy = 0.523204
Green Entropy = 0.52306
Blue Entropy = 0.567637
Average RGB Entropy = 0.537967

Red Entropy = 0.905032
Green Entropy = 0.941855
Blue Entropy = 0.870867
Average RGB Entropy = 0.905918

Red Entropy = 0.92939
Green Entropy = 0.983668
Blue Entropy = 0.980503
Average RGB Entropy = 0.96452

What the script does is as follows:

  • Computes the entropy from sum of -p*ln(p) for every graylevel
    where p=(count for a given graylevel)/(total pixels)
  • Normalizes the entropy by dividing by ln(255)

This is equivalent to the following IM commands for a graylevel image:

  • totpix=`convert $infile -ping -format "%[fx:w*h]" info:`
  • gray_entropy=$(convert $infile -channel r -separate -depth 8 -format "%c" histogram:info:- |\
    tr -cs '0-9\012' ' ' |\
    awk -v totpix="$totpix" '
    NF>1 { p = $1/totpix; gray_entropy += -p*log(p); }
    END { print gray_entropy/log(256); } ')
  • echo "Gray Entropy = $gray_entropy"