Fred's ImageMagick Scripts



    Licensing:

    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: http://www.imagemagick.org/script/license.php

AUTOCOLOR


Automatically color balances an image.

Download Script

last modified: November 06, 2014



USAGE: autocolor [-m method] [-c clipmode] [-l cliplow] [-h cliphigh] [-n neutralgray] infile outfile
USAGE: autocolor [-help]

-m .... method .......... method to adjust neutral color; method=gamma,
......................... recolor, none; default=gamma
-c .... clipmode ........ clip channels mode; clip=together or separate;
......................... default=together
-l .... cliplow ......... clip percent on low end of histogram;
......................... float; 0<=cliplow<=100; default=0.1
-h .... cliphigh ........ clip percent on high end of histogram;
......................... float; 0<=cliphigh<=100; default=same as cliplow
-n .... neutralgray ..... neutral gray value (percent); float between
......................... 0 and 100; default=mean of luminance

PURPOSE: To automatically color balance an image.

DESCRIPTION: AUTOCOLOR automatically color balance and image by shifting the mean value of each channel to the neutral gray value. There are three methods that can be used. The first uses -gamma computed from the neutral gray value and mean of each of the RGB channels. The second uses -recolor (or -color-matrix) computed from the neutral gray value and mean of each of the RGB channels. The third skips this step. In addition to corrrecting the neutral color, the histogram is stretched to full black and white using the clip values.

ARGUMENTS:

-m method ... METHOD defines how the neutral color will be set. The choices are gamma (g), recolor (r) or none (n). Method=gamma uses -gamma to shift the mean value to neutral gray. Method=recolor uses -recolor (-color-matrix) to shift the mean value to neutral gray. Method=none skips this processing and only does the clip operation. If method=none and clipmode=together, then the result of the script will be equivalent to IM -contrast-stretch with a change in contrast, but no color change. The default=gamma.

-c clipmode ... CLIPMODE specifies whether to clip the channels in unison or independently. The choices are: together (t) or separate (s). The default=together

Note: generally the best choices seem to be either method=recolor with clipmode=separate or method=gamma with clipmode=together.

-l cliplow ... CLIPLOW is the cumulative percent at the low end of the histogram whose graylevel will be stretch to full black. Values are floats between 0 and 100. If cliplow=0, then the stretch will locate the minimum value in the channel histogram. The default=0.1

-h cliphigh ... CLIPHIGH is the cumulative percent at the high end of the histogram whose graylevel will be stretch to full white. Values are floats between 0 and 100. If cliplow=0, then the stretch will locate the maximum value in the channel histogram. The default=same as cliplow.

-n neutralgray ... NEUTRALGRAY is the graylevel for a given channel to which the mean value will be adjusted. Values are floats between 0 and 100. The default is computed from the average of the luminance channel. Other choices are 50 for mid-gray or 84 which is equivalent to 18% neutral gray. Using larger values will make the resulting image brighter.

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 In Method And Clipmode
(purple border indicates best match to both Photoshop And GIMP)

Original Image

Photoshop Autocolor

GIMP Auto White Balance

Arguments:
-m recolor -c separate

Arguments:
-m gamma -c separate

Arguments:
-m none -c separate

Arguments:
-m recolor -c together

Arguments:
-m gamma -c together

Arguments:
-m none -c together



Variations In Method And Clipmode
(purple border indicates best match to both Photoshop And GIMP)

Original Image

Photoshop Autocolor

GIMP Auto White Balance

Arguments:
-m recolor -c separate

Arguments:
-m gamma -c separate

Arguments:
-m none -c separate

Arguments:
-m recolor -c together

Arguments:
-m gamma -c together

Arguments:
-m none -c together



Variations In Method And Clipmode
(red border indicates best match to Photoshop)
(blue border indicates best match to GIMP)

Original Image

Photoshop Autocolor

GIMP Auto White Balance

Arguments:
-m recolor -c separate

Arguments:
-m gamma -c separate

Arguments:
-m none -c separate

Arguments:
-m recolor -c together

Arguments:
-m gamma -c together

Arguments:
-m none -c together



Variations In Method And Clipmode
(red border indicates best match to Photoshop)
(blue border indicates best match to GIMP)
(Image obtained from http://www.webdesign.org/web/photoshop/photoshop-basics/photo-correction-101-auto-color.8149.html)

Original Image

Photoshop Autocolor

GIMP Auto White Balance

Arguments:
-m recolor -c separate

Arguments:
-m gamma -c separate

Arguments:
-m none -c separate

Arguments:
-m recolor -c together

Arguments:
-m gamma -c together

Arguments:
-m none -c together



Variations In Method And Clipmode
(red border indicates best match to Photoshop)
(blue border indicates best match to GIMP)

Original Image

Photoshop Autocolor

GIMP Auto White Balance

Arguments:
-m recolor -c separate

Arguments:
-m gamma -c separate

Arguments:
-m none -c separate

Arguments:
-m recolor -c together

Arguments:
-m gamma -c together

Arguments:
-m none -c together



What the script does is as follows:

  • Extracts the R,G,B channels of the image
  • Extracts the luminance channel of the image
  • Computes the average value of the luminance channel to use as neutral gray
  • Computes the average of each of the R,G,B channels
  • Computes a gamma value from the mean and neutral gray values for each channel or
    Computes a color ratio from the mean and neutral gray values for each channel
  • Applies -gamma to each channel of the image and recombines the channels or
    Applies -recolor to the original image using the color ratios
  • Applies -contrast-stretch with the clip percentages to the image or
    Separates the R,G,B channels again and applies -contrast-stretch to each channel,
    then recombines channels

This is equivalent to the following IM commands for the method=recolor clipmode=separate options.

  • convert $infile -colorspace RGB -channel R -separate $tmpR1
  • convert $infile -colorspace RGB -channel G -separate $tmpG1
  • convert $infile -colorspace RGB -channel B -separate $tmpB1
  • convert $infile -colorspace Rec609Luma $tmpL1
  • For each of the four tmp files get the mean in range 0-100% as follows:
  • mean=`convert $img -format "%[mean]" info:`
  • mean=`convert xc: -format "%[fx:100*$mean/quantumrange]" info:`
  • ngray=mean from $tmpL1
  • redratio=`convert xc: -format "%[fx:$ngray/$redmean]" info:`
  • greenratio=`convert xc: -format "%[fx:$ngray/$greenmean]" info:`
  • blueratio=`convert xc: -format "%[fx:$ngray/$bluemean]" info:`
  • convert $infile -recolor "$redratio 0 0 0 $greenratio 0 0 0 $blueratio" $infile
  • convert $infile -colorspace RGB -channel R -separate $tmpR1
  • convert $infile -colorspace RGB -channel G -separate $tmpG1
  • convert $infile -colorspace RGB -channel B -separate $tmpB1
  • convert $tmpR1 -contrast-stretch ${cliplow}%,${cliphigh}% $tmpR1
  • convert $tmpG1 -contrast-stretch ${cliplow}%,${cliphigh}% $tmpG1
  • convert $tmpB1 -contrast-stretch ${cliplow}%,${cliphigh}% $tmpB1
  • convert $tmpR1 $tmpG1 $tmpB1 -combine $outfile