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

BALANCE


Enhances the shadows, midtones and highlight regions of the image.

Download Script

last modified: September 26, 2017



USAGE: balance "shadows,midtones,highlights" [-b bias] infile outfile
USAGE: balance [-h or -help]

"shadows,midtones,highlights" .... percent change in values in shadows,
.................................. midtones, highlight areas in the image;
.................................. positive or negative floats
-b ........... bias .............. percent shift in midtone location;
.................................. positive or negative floats; default is
.................................. no change from mean of image.

PURPOSE: Enhances the shadows, midtones and highlight regions of the image.

DESCRIPTION: BALANCE enhances the shadows, midtones and highlight regions of the image. This is done according to a three point piece-wise linear transformation, where the three points are at input graylevels: 0, image mean, 255 (in 8-bit system). The transformation is applied as a look up table using -clut.

ARGUMENTS:

"shadows,midtones,highlights" ... Percent change in values in the shadows, midtones and highlight areas of the image. Values are positive or negative floats. A value of "0,0,0" means the output will be the same as the input.

-b bias ... BIAS is the percent shift of the midtone location from the mean value of the input. Values are positive or negative floats. The default=0 indicates no change from the global mean value of all channels of the input image.

REQUIREMENT: IM version 6.3.5-7 or higher due to the use of -clut.

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


Shadows

Original

arguments:
-25,0,0

arguments:
25,0,0

Transformation



Midtones

Original

arguments:
0,-25,0

arguments:
0,25,0

Transformation



Highlights

Original

arguments:
0,0,-25

arguments:
0,0,25

Transformation



Combinations

Original

arguments:
-25,0,25

arguments:
-25,10,25 -b 10

Transformation



What the script does is as follows:

  • Creates a two-part gradient from 3 point pairs as a lookup table
  • Applies the lookup table to the image using -clut

This is equivalent to the following IM commands:

  • midx=`convert $infile -format "%[fx:round(255*(mean+$bias/100))]" info:`
  • midy=`convert xc: -format "%[fx:$midx/255]" info:`
  • numvals=`echo $values | tr "," " " | wc -w`
  • values=`echo $values | sed 's/[ ]*//g'`
  • vals=`echo $values | cut -d, -f 1`
  • valm=`echo $values | cut -d, -f 2`
  • valh=`echo $values | cut -d, -f 3`
  • vals=`convert xc: -format "%[fx:$vals/100]" info:`
  • valm=`convert xc: -format "%[fx:$midy+$valm/100]" info:`
  • valh=`convert xc: -format "%[fx:1+$valh/100]" info:`
  • x1=0
  • y1=$vals
  • x2=$midx
  • y2=$valm
  • x3=255
  • y3=$valh
  • convert -size 256x1 xc: -fx \
    "(i>=0&&i<$midx)?(i-$x1)*($y2-$y1)/($x2-$x1)+$y1:(i-$x2)*($y3-$y2)/($x3-$x2)+$y2" \
    $tmpA2
  • convert $infile $tmpA2 -clut $outfile