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

DUALTONEMAP


Enhances the shadows and/or highlight regions in an image.

Download Script

last modified: September 26, 2017



USAGE: dualtonemap [-c] [-g gamma] [-m mode] [-b bias] [-t thresh] [-s svals] [-h hvals] [-r ramp] infile outfile
USAGE: dualtonemap [-help]

-c .............. set colorspace of input image from sRGB to RGB
................. before processing
-g ... gamma .... apply non-linear gamma processing to input image before
................. processing. Alternate to -c option. Nominal value is
................. gamma=2. Values are floats>0; default=1.
-m ... mode ..... mode of operation; choices are: shadows, highlights or
................. both; default=both
-b ... bias ..... percent shift in mean value used as threshold between
................. shadows and highlights; positive or negative floats;
................. default is no change from mean of image.
-t ... thresh ... threshold value between shadows and highlights;
................. 0<=float<=100; overrides automatic mode of mean of
................. image if provided
-s ... svals .... blackpoint,whitepoint,gamma values to use in shadows;
................. 0<=float<=100 for blackpoint and whitepoint
................. and float>0 for gamma; svals=0,100,1 is no change;
................. default=0,50,1
-h ... hvals .... blackpoint,whitepoint,gamma values to use in highlights;
................. 0<=float<=100 for blackpoint and whitepoint
................. and float>0 for gamma; hvals=0,100,1 is no change;
................. default=50,100,1
-r ... ramp ..... ramp of transition between shadows and highlights in
................. pixels; integer>=0; default=50

PURPOSE: Enhances the shadows and/or highlight regions in an image.

DESCRIPTION: DUALTONEMAP enhances the shadows and/or highlight regions in an image. This is done by adjusting the -level blackpoint,whitepoint% in the shadows and/or highlight regions. This is similar to Photoshop's Shadows/Highlights function.

ARGUMENTS:

-c ... Set colorspace of input image from sRGB to RGB before processing. This provides an initial non-linear processing. The default=no.

-g gamma ... GAMMA applies a non-linear gamma processing to input image before processing. Alternate to -c option. Do not use both. Values are floats>0. Nominal value is gamma=2. The default=1 (no non-linear gamma processing).

-m mode ... MODE of operation that specifies to adjust shadows or highlights or both. Choices are shadows (or s), highlights (or h) or both (or b). The default=both

-b bias ... BIAS is the percent shift of the mean value of the input that is is used as the nominal threshold value between shadows and highlights. Values are positive or negative floats. The default=0 indicates no change from the global mean value of all channels of the input image.

-t thresh ... THRESH is the user specified threshold value. When used, it overrides the automatic value from the (mean + bias) value. Values are floats between 0 and 100. The default is to use automatic value from the (mean + bias).

-s svals ... SVALS=blackpoint,whitepoint,gamma used by -level in the shadows region. Values of each blackpoint and whitepoint are float percents in the range of 0 to 100 and gamma is a float>0. A value of svals=0,100,1 produces no change. The default=0,50,1.

-h hvals ... HVALS=blackpoint,whitepoint,gamma used by -level in the highlights region. Values of each blackpoint and whitepoint are float percents in the range of 0 to 100 and gamma is a float>0. A value of hvals=0,100,1 produces no change. The default=50,100,1.

-r ramp ... RAMP is the transition distance in pixels between the shadows and highlights. Values are integers>=0. The default=50.

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

Example 1

Original

-m shadows -s 0,30

-m shadows -s 0,100 -c



Example 2

Original

Arguments:
-m shadows -s 0,40

Arguments:
-m shadows -s 0,100 -c



Example 3

Original

Arguments:
-m shadows -s 0,30

Arguments:
-m shadows -s 0,100,0.9 -c



Example 4

Original

Arguments:
-m shadows -s 0,30

Arguments:
-m shadows -s 0,80 -c



Example 5

Original

Arguments:
-m both -s 0,30,2 -h 20,100

Arguments:
-m both -s 0,70 -h 50,100 -c

Arguments:
-m both -s 0,70,1.5 -h 50,100 -c



Example 6

Original

Arguments:
-m shadows -s 0,8

Arguments:
-m both -s 0,45 -h 50,100 -c



Example 7

Original
(Actual HDR Format Used)

Arguments:
-m both -s 0,18 -h 18,100 -c -r 3



Example 8

Original
(Actual HDR Format Used)

Arguments:
-m shadows -s 0,25 -c -r 3

Arguments:
-m shadows -s 0,30 -c -r 3 -t25



What the script does is as follows:

  • Computes the mean of the image
  • Thresholds the image at the mean
  • Blurs the thresholded image over the ramp distance
  • Applies the highlight -level parameters to the input image
  • Applies the shadow -level parameters to the input image
  • Composites the two modified images using the mask image
    to generate the output

This is equivalent to the following IM commands

  • if [ "$cmode" = "yes" ]; then
    colormode="-set colorspace sRGB -colorspace RGB"
    else
    colormode=""
    fi
  • convert -quiet -regard-warnings "$infile" $colormode +repage "$tmpA1"
  • thresh=`convert $tmpA1 -format "%[fx:100*(mean+$bias/100)]" info:`
  • convert $tmpA1 -threshold ${thresh}% -blur ${ramp}x65000 $tmpA2
  • if [ "$mode" = "both" ]; then
    convert $tmpA1 -level ${sbp},${swp},${sg}% $tmpA3
    convert $tmpA1 -level ${hbp},${hwp},${hg}% $tmpA4
    convert $tmpA3 $tmpA4 $tmpA2 -compose over -composite $outfile
    elif [ "$mode" = "shadows" ]; then
    convert $tmpA1 -level ${sbp},${swp},${sg}% $tmpA3
    convert $tmpA3 $tmpA1 $tmpA2 -compose over -composite $outfile
    elif [ "$mode" = "highlights" ]; then
    convert $tmpA1 -level ${hbp},${hwp},${hg}% $tmpA4
    convert $tmpA1 $tmpA4 $tmpA2 -compose over -composite $outfile
    fi