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

DUOTONEMAP


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

Download Script

last modified: September 26, 2017



USAGE: duotonemap [-m mode] [-b bias] [-t thresh] [-s sgamma] [-h hgamma] [-r ramp] [-l lower] [-u upper] infile outfile
USAGE: duotonemap [-help]

-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 .... sgamma .... gamma value to use in shadows; float>0; sgamma=1 is
................... no change; values larger/smaller than 1 produce
................... brighter/darker results; default is computed
................... automatically from shadow area mean and "lower"
................... graylevel parameter.
-h .... hgamma .... gamma value to use in highlights; float>0; sgamma=1 is
................... no change; values larger/smaller than 1 produce
................... brighter/darker results; default is computed
................... automatically from shadow area mean and "upper"
................... graylevel parameter.
-r .... ramp ...... ramp of transition between shadows and highlights in
................... pixels; integer>=0; default=20
-l .... lower ..... lower graylevel value used in automatic sgamma
................... computation; smaller/larger values produce
................... darker/larger results; 0<=float<=1; default=0.5
-u .... upper ..... upper graylevel value used in automatic hgamma
................... computation; smaller/larger values produce
................... darker/larger results; 0<=float<=1; default=0.7

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

DESCRIPTION: DUOTONEMAP enhances the shadows and/or highlight regions in an image. This is done by adjusting the gamma in each region as defined by a threshold of the image used as a mask. Processing is nominally automatic, but can be overridden with manual settings. This is similar to Photoshop's Shadows/Highlights function.

ARGUMENTS:

-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 sgamma ... SGAMMA is the gamma value to use in the shadow region. When provided it overrides the automatic value determined from the mean value in the shadows and the "lower" graylevel parameter. Values are floats>0. A value of sgamma=1 produces no change. Smaller/larger values produce darker or brighter results in the shadows. The default is to use the automatically computed value.

-h hgamma ... HGAMMA is the gamma value to use in the highlight region. When provided it overrides the automatic value determined from the mean value in the highlights and the "upper" graylevel parameter. Values are floats>0. A value of hgamma=1 produces no change. Smaller/larger values produce darker or brighter results in the highlights. The default is to use the automatically computed value.

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

-l lower ... LOWER is the graylevel value used with the mean value of the shadows to compute the shadow gamma. Values are in the range 0<=float<=1. Smaller/larger values produces darker/brighter results. The default=0.5

-u upper ... UPPER is the graylevel value used with the mean value of the highlights to compute the highlight gamma. Values are in the range 0<=float<=1. Smaller/larger values produces darker/brighter results. The default=0.7

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

Arguments:
defaults

redist 40,70,70

-auto-level



Example 2

Original

redist 50,70,70

Arguments:
-m both
(default)

Arguments:
-m shadow



Example 3

Original

redist 50,50,40

Arguments:
-m both
(default)

Arguments:
-m shadow

Arguments:
-m both -s 4 -h 1

Arguments:
-m both -s 6 -h 1.1



Example 4

Original

Arguments:
(default)

redist 60,60,60



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 mask to the image and computes the mean and
    gamma value for the highlights
  • Applies the negated mask to the image and computes the mean and
    gamma value for the shadows
  • Applies the highlight gamma to the input image
  • Applies the shadow gamma to the inputimage
  • Composites the two gamma modified images using the mask image
    to generate the output

This is equivalent to the following IM commands

  • thresh=`convert $infile -format "%[fx:100*(mean+$bias/100)]" info:`
  • convert $infile -threshold ${thresh}% -blur ${ramp}x65000 $tmpA2
  • meanm=`convert $tmpA2 -format "%[fx:mean]" info:`
  • means=`convert $infile \( $tmpA2 -negate \) -compose multiply -composite \
    -format "%[fx:mean/(1-$meanm)]" info:`
  • meanh=`convert $tmpA1 $tmpA2 -compose multiply -composite \
    -format "%[fx:mean/$meanm]" info:`
  • sgamma=`convert xc: -format "%[fx:log($means)/log($lower)]" info:`
  • hgamma=`convert xc: -format "%[fx:log($meanh)/log($upper)]" info:`
  • convert $infile -auto-level -gamma $sgamma $tmpA3
  • convert $infile -auto-level -gamma $hgamma $tmpA4
  • convert $tmpA3 $tmpA4 $tmpA2 -compose over -composite $outfile