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.

AUTOWHITE


Automatically adjusts the white balance of an image.

Download Script

last modified: December 15, 2018



USAGE: autowhite [-m method] [-p percent] infile outfile
USAGE: autowhite [-help]

-m .... method .......... method to adjust white balance;
......................... method=1 is multiplicative adjust;
......................... method=2 is additivie adjust; default=1
-p .... percent ......... percent pixels closest to white to get average;
......................... float; 0<=percent<=100; default=1

PURPOSE: To automatically adjust the white balance of an image.

DESCRIPTION: AUTOWHITE automatically adjusts the white balance of an image. Two methods are available. Method 1 uses a multiplicative adjustment using -recolor (-color-matrix). Method 2 uses an additive adjustment using -evaluate add. Both methods compute RGB channel averages of a user specified percentage of pixels closest to white. The channel averages are used in a ratio compared with white in method 1 and as a difference from white in method 2.

ARGUMENTS:

-m method ... METHOD defines how the white balance adjustment will be handled. Method 1 uses a multiplicative adjustment using -recolor (-color-matrix), where the matrix values are the ratios of 100% to the channel averages in percent graylevel. Method 2 uses an additive adjustment using -evaluate add, where the additive amount is the percent difference of the channel averages from 100%. Method 1 is generally superior. Method 2 often shifts the overall color oddly. The default is method=1.

-p percent ... PERCENT is the percentage of pixels closest in color to white that is used to compute the average graylevel of each RGB channel in the image. Values are floats between 0 and 100. Default=1

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


Variation In Percent Pixels Used To Compute Average

Original Image

Arguments:
-m 1 -p 0

Arguments:
-m 1 -p 1

Arguments:
-m 1 -p 5

Original Image

Arguments:
-m 2 -p 0

Arguments:
-m 2 -p 1

Arguments:
-m 2 -p 5



Comparison Method 1 And 2 --- Percent=1

Original Image

Arguments:
-m 1 -p 1

Arguments:
-m 2 -p 1



Comparison Method 1 And 2 --- Percent=1

Original Image

Arguments:
-m 1 -p 1

Arguments:
-m 2 -p 1



Comparison Method 1 And 2 --- Percent=1

Original Image

Arguments:
-m 1 -p 1

Arguments:
-m 2 -p 1



Comparison Method 1 And 2 --- Percent=1

Original Image

Arguments:
-m 1 -p 1

Arguments:
-m 2 -p 1



What the script does is as follows:

  • Converts the image to RGB and separates channels
  • Creates a mask from the negated saturation channel
    multiplied by the brightness channel from HSB
    so that the most intense 1% (default) pixels are white
    and the rest black
  • Multiplies the mask image by each channel image
  • Computes the mean of the mask image (maskmean) and the
    mean of each masked channel image (mean)
  • Computes the ratio of maskmean/mean for each channel
  • Applies -recolor to the original image using the ratios
    for the main diagonal elements of the matrix and zero elsewhere

This is equivalent to the following IM commands for method=1.

  • 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 $tmpA1 -colorspace HSB -channel G -negate -channel GB -separate +channel \
    -compose multiply -composite +channel \
    -contrast-stretch 0,${percent}% -fill black +opaque white \
    $tmpM1
  • maskmean=`convert $tmpM1 -format "%[mean]" info:`
  • convert $tmpR1 $tmpM1 -compose multiply -composite $tmpT1
  • mean=`convert $tmpT1 -format "%[mean]" info:`
  • redratio=`convert xc: -format "%[fx:$maskmean/$mean]" info:`
  • convert $tmpG1 $tmpM1 -compose multiply -composite $tmpT1
  • mean=`convert $tmpT1 -format "%[mean]" info:`
  • greenratio=`convert xc: -format "%[fx:$maskmean/$mean]" info:`
  • convert $tmpB1 $tmpM1 -compose multiply -composite $tmpT1
  • mean=`convert $tmpT1 -format "%[mean]" info:`
  • blueratio=`convert xc: -format "%[fx:$maskmean/$mean]" info:`
  • convert $tmpA1 -recolor "$redratio 0 0 0 $greenratio 0 0 0 $blueratio" $outfile