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.

MIDTONEBALANCE


Color balances an image in the midtones in a manner similar to Photoshop

Download Script

last modified: December 15, 2018



USAGE: midtonebalance [-rc red-cyan] [-gm green-magenta] [-by blue-yellow ] [-p] infile outfile
USAGE: midtonebalance [-h or -help]

-rc ... red-cyan ........ controls the red-cyan color balance; nominal values
......................... are -100<=integer<=100; positive values apply
......................... more red; negative values apply more cyan;
......................... default=0 (no change)
-gm ... green-magenta ... controls the green-magenta color balance; nominal
......................... values are -100<=integer<=100; positive values
......................... apply more green; negative values apply more
......................... magenta; default=0 (no change)
-by ... blue-yellow ..... controls the blue-yellow color balance; nominal
......................... values are -100<=integer<=100; positive values
......................... apply more blue; negative values apply more
......................... yellow; default=0 (no change)
-p ...................... preserves luminosity

PURPOSE: To color balance an image in the midtones.

DESCRIPTION: MIDTONEBALANCE color balances an image in the midtones in a manner similar to Photoshop.

ARGUMENTS:

-rc (or -r) red-cyan ... RED-CYAN controls the red-cyan color balance. Nominal values are -100<=integer<=100. Positive values apply more red. Negative values apply more cyan. The default=0 (no change)

-gm (or -g) green-magenta ... GREEN-MAGENTA controls the green-magenta color balance. Nominal values are -100<=integer<=100. Positive values apply more green. Negative values apply more magenta. The default=0 (no change)

-by (or -b) blue-yellow ... BLUE-YELLOW controls the blue-yellow color balance. Nominal values are -100<=integer<=100. Positive values apply more blue. Negative values apply more yellow. The default=0 (no change)

-p ... preserves luminosity

LIMITATIONS: Preserve luminosity works best (matches better to Photoshop) for IM 6.8.5.5 or higher with the introduction of colorspace HCLp.

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

Original Image

Arguments:
-rc 100
(red)

Arguments:
-rc -100
(cyan)

Arguments:
-gm 100
(green)

Arguments:
-gm -100
(magenta)

Arguments:
-by 100
(blue)

Arguments:
-by -100
(yellow)

Arguments:
-rc 100 -p
(red)

Arguments:
-rc -100 -p
(cyan)

Arguments:
-gm 100 -p
(green)

Arguments:
-gm -100 -p
(magenta)

Arguments:
-by 100 -p
(blue)

Arguments:
-by -100 -p
(yellow)



Variation In Strength For Color Blue

Original Image

Arguments:
-by 50

Arguments:
-by 100

Arguments:
-by 150

Arguments:
-by 50 -p

Arguments:
-by 100 -p

Arguments:
-by 150 -p



What the script does is as follows:

  • Creates a 1D image look-up table (lut) for each r,g,b channel using a -fx equation
  • Combines the 3 luts into one color lut
  • Applies it to the image
  • If preserve luminosity is enabled, then a compose luminize operation is added

This is equivalent to the following IM commands:

  • convert -quiet -regard-warnings "$infile" $tmpA1
  • convert -size 1x1024 gradient: -rotate 90 $tmpL
  • if [ $red -gt 0 ]; then
    convert $tmpL -channel r \
    -fx "u+0.15*(1/(1*u+0.2))*($red/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    elif [ $red -lt 0 ]; then
    convert $tmpL -channel r \
    -fx "u+0.25*($red/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    fi
  • if [ $green -gt 0 ]; then
    convert $tmpL -channel g \
    -fx "u+0.15*(1/(1*u+0.2))*($green/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    elif [ $green -lt 0 ]; then
    convert $tmpL -channel g \
    -fx "u+0.25*($green/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    fi
  • if [ $blue -gt 0 ]; then
    convert $tmpL -channel b \
    -fx "u+0.15*(1/(1*u+0.2))*($blue/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    elif [ $blue -lt 0 ]; then
    convert $tmpL -channel b \
    -fx "u+0.25*($blue/100)*(1-(4.0*((u-0.5)*(u-0.5))))" +channel $tmpL
    fi
  • if [ "$preserveluminosity" = "no" ]; then
    convert $tmpA1 $tmpL -clut $outfile
    else
    convert $tmpA1 $tmpL -clut $tmpA2
    convert $tmpA2 \
    \( $tmpA1 $setcspace -colorspace HCLp -channel b -separate +channel \) \
    -compose Luminize -composite $outfile
    fi