Fred's ImageMagick Scripts



 

 

STRETCH


Modifies an image to automatically stretch the dynamic range between full black and white.

Download Script

last modified: July 28, 2009



USAGE: stretch [-c colormode] infile outfile
USAGE: stretch [-h or -help]

-c .... colormode ..... colorspace/channel to use to compute
....................... min and max statistics; choices are:
....................... global, intensity, luminance, lightness,
....................... rgb and sl; default=luminance

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

DESCRIPTION: STRETCH modifies an image to automatically stretch the dynamic range between full black and white. No gamma correction is applied. The minimum and maximum 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(s) to use to compute the minimum and maximum values. The choices are: global, intensity, luminance, lightness, rgb and sl. The default is luminance.

Global uses aggregate statistics from all the channels. Intensity uses statistics from -colorspace Gray (Rec609Luma). Luminance uses statistics from -colorspace Rec709Luma. Lightness uses statistics from the lightness channel of -colorspace HSL. RGB uses statistics independently from each channel of -colorspace RGB. SL uses statistics independently from each the S,L channels of -colorspace HSL. See definitions at: http://www.imagemagick.org/script/command-line-options.php#colorspace

Note: generally there are only slight differences between the various non-rgb colormode results. Colormode=rgb can cause color balance shifts.

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


Variations On Colormode In Low Contrast Image

Original Image

Arguments:
-c global

Arguments:
-c intensity

Arguments:
-c luminance

Arguments:
-c lightness

Arguments:
-c sl

Arguments:
-c rgb



Variations On Colormode In Low Contrast Image
(Image obtained from http://www.webdesign.org/web/photoshop/photoshop-basics/photo-correction-101-auto-color.8149.html)

Original Image

Arguments:
-c global

Arguments:
-c intensity

Arguments:
-c luminance

Arguments:
-c lightness

Arguments:
-c ls

Arguments:
-c rgb



What the script does is as follows:

  • Converts the image to the desired colorspace/channel(s)
  • Generates a copy of the image in the desired colorspace/channel(s)
  • Accesses the minimum and maximum for this colorspace/channel(s)
  • Passes the minimum and maximum values to -level and applies to
    each channel
  • Converts back to RGB if necessary

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

  • convert $tmpA1 -colorspace Rec709Luma $tmpI1
  • min=`convert $tmpI1 -format "%[min]" info:`
  • max=`convert $tmpI1 -format "%[max]" info:`
  • min=`convert xc: -format "%[fx:100*$min/quantumrange]" info:`
  • max=`convert xc: -format "%[fx:100*$max/quantumrange]" info:`
  • convert $tmpA1 -level ${min}%,${max}% $outfile