Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) 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: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

AUTOLEVEL


Modifies an image to automatically stretch the dynamic range between full black and white and automatically apply a gamma correction.

Download Script

last modified: December 15, 2018



USAGE: autolevel [-c colormode] [-m midrange] infile outfile
USAGE: autolevel [-h or -help]

-c .... colormode ..... colorspace/channel to use to compute
....................... min, max, gamma statistics; choices are:
....................... gray, intensity, luminance, lightness, brightness,
....................... average, magnitude, rgb; default=luminance
-m .... midrange ...... midrange value for autogamma part of script;
0<float<1; default=0.5

PURPOSE: To modify an image to automatically stretch the dynamic range between full black and white and automatically apply a gamma correction.

DESCRIPTION: AUTOLEVEL modifies an image to automatically stretch the dynamic range between full black and white and automatically apply a gamma correction. The minimum, maximum and gamma values may be computed from various graylevel representations of the image or individually channel-by-channel. The script then passes these values to the IM function -level.

ARGUMENTS:

-c colormode ... COLORMODE is the colorspace/channel to use to compute the minimum, maximum and gamma values. The choices are: gray, intensity, luminance, lightness, brightness, average, magnitude and rgb. Values of gray and intensity are equivalent. The default is luminance.

Gray or Intensity uses statistics from -colorspace Gray.
Luminance uses statistics from -colorspace Rec709Luma.
Lightness uses statistics from the lightness channel of -colorspace HSL.
Brightness uses statistics from the brightness channel of -colorspace HSB.
Average uses statistics from the first channel of -colorspace OHTA.
Magnitude uses aggregate statistics from all the channels.
RGB uses statistics independently from each channel of -colorspace sRGB/RGB.
See definitions at: http://www.imagemagick.org/script/command-line-options.php#colorspace

-m midrange ... MIDRANGE is the midrange value for the autogamma technique, which uses the formula: Gamma = log(mean)/log(mid-dynamic-range). Values are in the range 0

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.


EXAMPLES


Colormode Results On Dark Image

 

Original Image

Arguments:
-c rgb

 
   

Arguments:
-c luminance

Arguments:
-c gray (or intensity)

Arguments:
-c average

Arguments:
-c lightness

Arguments:
-c magnitude

Arguments:
-c brightness



Variation Of Internal Midrange Parameter

Original Dark Image

Arguments:
-c gray
(midrange=.5)

Arguments:
-c gray
(midrange=.55)

Arguments:
-c gray
(midrange=.6)

Original Bright Image

Arguments:
-c gray
(midrange=.5)

Arguments:
-c gray
(midrange=.55)

Arguments:
-c gray
(midrange=.6)



The Following Images Were Obtained At http://images.google.com/


Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



Original Image

Arguments:
-c luminance



What the script does is as follows:

  • Converts the image to RGB
  • Generates a copy of the image in the desired colorspace/channel
  • Accesses the minimum, maximum and mean statistics for this image
  • Computes a gamma value from the mean
  • Passes the minimum, maximum and gamma values to -level and applies to
    each channel of the RGB image

This is equivalent to the following IM commands for the luminance colormode.

  • convert $infile -colorspace RGB $tmp1
  • convert $tmp1 -colorspace Rec709Luma $tmp2
  • min=`convert $tmp2 -format "%[min]" info:`
  • max=`convert $tmp2 -format "%[max]" info:`
  • mean=`convert $tmp2 -format "%[mean]" info:`
  • min=`convert xc: -format "%[fx:100*$min/quantumrange]" info:`
  • max=`convert xc: -format "%[fx:100*$max/quantumrange]" info:`
  • mean=`convert xc: -format "%[fx:100*$mean/quantumrange]" info:`
  • gammaval=`convert xc: -format "%[fx:log($mean/100)/log($midrange)]" info:`
  • convert $tmp1 -level ${min}%,${max}%,${gammaval} $outfile