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

COLORBALANCE


Manually color balances an image in midtones, highlights, shadows or overall.

Download Script

last modified: April 25, 2015



USAGE: colorbalance [-c color] [-a amount] [-r region] [-m mid] [-f factor] [-l low] [-h high] [-t taper] infile outfile
USAGE: colorbalance [-help]

-c .... color ..... color to modify: red (r), yellow (y), green (g),
................... cyan (c), blue (b), magenta (m); default=red
-a .... amount .... amount of color change; 0<=float<=100; default=10
-r .... region .... region to change: midtones (m), shadows (s),
................... highlights (h), all (a); default=all
-m .... mid ....... mid value threshold; 0<=float<=100; default=mean
-l .... low ....... low value threshold; 0<=float<=100;
................... default=mean-factor*std
-h .... high ...... high value threshold; 0<=float<=100;
................... default=mean+factor*std
-f .... factor .... standard deviation factor; float>0; default=0.5
-t .... taper ..... taper for range thresholds in pixels; integer>=0;
................... default=10

PURPOSE: To manually color balance an image in midtones, highlights, shadows or overall.

DESCRIPTION: COLORBALANCE manually color balances an image according to a user selected color, region and amount. The regions are midtones, shadows, highlights or all (i.e. the whole image). The regions thresholds default to mean +/- factor*standard-deviation of the appropriate channel or can be set manually. The region boundaries can be tapered so that they change gradually.

ARGUMENTS:

-c color ... COLOR to modify. The choices are: red (r), yellow (y), green (g), cyan (c), blue (b) or magenta (m). The default=red

-a amount ... AMOUNT of color change. Values are in the range 0<=float<=100. The default=10.

-r region ... REGION of image to change. The choices are: midtones (m), shadows (s), highlights (h) or all (a) for the whole image. The default=all.

-m mid ... MID is the threshold value that determines the center of the midtone range. Values are in the range 0<=float<=100. The default is the appropriate channel mean.

-l low ... LOW is the threshold value that determines the low end of the midtones and the high end of the shadows. Values are in the range 0<=float<=100. The default is the appropriate channel mean - factor*standard-deviation

-h high ... HIGH is the threshold value that determines the high end of the midtones and the low end of the highlights. Values are in the range 0<=float<=100. The default is the appropriate channel mean + factor*standard-deviation

-f factor ... FACTOR is the multiplication factor for the standard deviation used for the low and high threshold values. Values are floats>0. The default=0.5.

-t taper ... TAPER is the taper for range the thresholds in pixels. Values are integers>=0. The default=10

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 Color Over Whole Image

Original Image

Arguments:
-c red -a 25

Arguments:
-c yellow -a 25

Arguments:
-c green -a 25

Arguments:
-c cyan -a 25

Arguments:
-c blue -a 25

Arguments:
-c magenta -a 25



Variation In Range For Color Red

Original Image

Arguments:
-c red -a 10 -r all

Arguments:
-c red -a 10 -r midtones

Arguments:
-c red -a 10 -r highlights

Arguments:
-c red -a 10 -r shadows



What the script does is as follows:

  • Selects the appropriate channel
  • Uses -level to process that channel
  • Creates a mask for the desired region as appropriate
  • Uses the mask to composite the processed and original images

This is equivalent to the following IM commands for the case of midtones:

  • convert -quiet -regard-warnings "$infile" $dir/tmpI.mpc
  • case "$color" in
    red) color2="red" ;;
    yellow) color2="blue" ;;
    green) color2="green" ;;
    cyan) color2="red" ;;
    blue) color2="blue" ;;
    magenta) color2="green" ;;
    esac
  • case "$color" in
    red) color3="r" ;;
    yellow) color3="b" ;;
    green) color3="g" ;;
    cyan) color3="r" ;;
    blue) color3="g" ;;
    magenta) color3="b" ;;
    esac
  • if [ "$color" = "red" -o "$color" = "green" -o "$color" = "blue" ]; then
    amount=`convert xc: -format "%[fx:100-$amount]" info:`
    process="-channel $color2 -level 0x${amount}% +channel"
    else
    process="-channel $color2 -level ${amount}x100% +channel"
    fi
  • mean=`convert $dir/tmpI.mpc -format "%[fx:mean.$color3]" info:`
  • std=`convert $dir/tmpI.mpc -format "%[fx:standard_deviation.$color3]" info:`
  • [ "$mid" = "" ] && mid=`convert xc: -format "%[fx:100*$mean]" info:`
  • [ "$low" = "" ] && low=`convert xc: -format "%[fx:100*($mean-$factor*$std)]" info:`
  • [ "$high" = "" ] && high=`convert xc: -format "%[fx:100*($mean+$factor*$std)]" info:`
  • convert $dir/tmpI.mpc \( -clone 0 $process \) \
    \( -clone 0 -channel $color2 -separate +channel \
    -white-threshold ${high}% -fill black -opaque white \
    -black-threshold ${low}% -fill white +opaque black \
    -blur ${taper}x65000 -level 0x50% \) \
    -compose over -composite "$outfile"