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.

MODULATECOLOR2


Changes brightness, saturation and/or hue for any primary or secondary color in an image.

Download Script

last modified: December 15, 2018



USAGE: modulatecolor2 [-c color] [-b brightness] [-s saturation] [-u units] [-h hue] [-m mode] [-t tolerance ] [-r ramping] [-C colorspace] infile outfile
USAGE: modulatecolor2 [-help]

-c ... color ........ color to modify: red (r), yellow (y), green (g),
..................... cyan (c), blue (b), magenta (m); default=red
-b ... brightness ... percent brightness; float>=0; default=100 (no change)
-s ... saturation ... percent saturation; float>=0; default=100 (no change)
-u ... units ........ units for hue; percent (p) or degrees (d);
..................... default=percent
-h ... hue .......... hue value 0<=float<=100 percent or 0<=float<=360
..................... degrees; default=0 (red stays red)
-m ... mode ......... mode for hue values; choices are: relative (r) or
..................... absolute (a); default=absolute
-t ... tolerance .... tolerance (fuzz value) on each side of color for range
..................... of hue change as specified by units; 0<=float<=180
..................... degrees or 0<=flaot<=50 percent; default=30 degrees.
-r ... ramping ...... ramping (tapering) of new hue in the form of a Gaussian
..................... rolloff; value is the Gaussian sigma in the specified
..................... units; float>=0; default=0 (no ramping)
-C ... colorspace ... colorspace in which to modulate the image; choices are:
..................... HSL or HCLp; default=HSL

PURPOSE: To change brightness, saturation and/or hue for any primary or secondary color in an image.

DESCRIPTION: MODULATECOLOR2 changes the brightness and/or saturation and/or hue for any primary or secondary color in an image. Hues are arranged in a cyclical order in the range 0 to 100 percent or 0 to 360 degrees. Hue may be changed any amount in those ranges per the units specified. The colors allowed are: red, yellow, green, cyan, blue and magenta. Units for hue may be either percent or degrees.

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.

-b brightness ... BRIGHTNESS is the absolute percent brightness. Values are floats>=0. The default=100 (no change).

-s saturation ... SATURATION is the absolute percent saturation. Values are floats>=0. The default=100 (no change).

-u units ... UNITS for hue. Choices are: percent (p) or degrees (d). The default=degrees.

-m mode ... MODE for hue values. The choices are: relative (r) or absolute (a). Relative means the replacement hue value will be a shift relative to the specified color. Absolute means the hue value will be the exact hue value that will be used to replace the specified color. The default=absolute.

-t tolerance ... TOLERANCE (fuzz value) on each side of the specified color for the range of the hue change as specified by the selected units. Values are 0<=float<=180 for degrees or 0<=flaot<=50 for percent. The default=30 degrees. Note that tolerance less than about 0.1 degree do not work well.

-r ramping ... RAMPING (tapering) of the new hue in the form of a Gaussian rolloff. The value is the Gaussian sigma in the specified units. Values are floats>=0. The default=0 (no ramping).

-h hue ... HUE is the absolute hue value to be used to replace the hue of the specified color. Values are 0<=floats<=100 for percent or 0<=floats<=360 for degrees. The default=0 (red stays red). Hue change is cyclical in the range from 0 to either 100 percent or 360 degrees. So a hue value of 100 percent or 360 degrees is equivalent to 0 or no change.

-C colorspace ... COLORSPACE is the colorspace in which to modulate the image. The choices are: HSL or HCLp.

REQUIREMENTS: IM 6.5.3-7 so that -modulate uses HSL and not HSB colorspace. HCLp modulation is available as of version 6.8.6-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.


REFERENCE CHART


EXAMPLES


Example 1 - With And Without Ramping

Original Image

Arguments:
-c green -u degrees -m absolute -h 240 -t 30 -r 0
Arguments:
-c green -u degrees -m absolute -h 240 -t 30 -r 10


Example 2 - Variation In Tolerance

Original Image

Arguments:
-c green -u degrees -m absolute -h 240 -t 60 -r 0
Arguments:
-c green -u degrees -m absolute -h 240 -t 30 -r 0
Arguments:
-c green -u degrees -m absolute -h 240 -t 10 -r 0
Arguments:
-c green -u degrees -m absolute -h 240 -t 1 -r 0
Arguments:
-c green -u degrees -m absolute -h 240 -t 0.1 -r 0


Example 3

Original Image
(source)

Arguments:
-c green -u degrees -m absolute -h 0 -s 300 -t 50 -r 10
Arguments:
-c green -u degrees -m absolute -h 180 -s 300 -t 50 -r 10


What the script does is as follows:

  • Creates a 1D lut image in accordance with the new color, tolerance and ramping
  • Clones the input and modulates it
  • Clones the input image, converts to the specified colorspace, then
    selects the hue channel and applies the lut to create a mask image
  • Composites the original and modulated image together using the mask image

This is equivalent to the following IM commands for the case of color=green, units=degrees and mode=absolute.

  • hue=`convert xc: -format "%[fx:100+(200*($hue-120)/360)]" info:`
  • full_range=`convert xc: -format "%[fx:2*round($tolerance*100)-1]" info:`
  • sigma=`convert xc: -format "%[fx:$ramping*100]" info:`
  • rollval=`convert xc: -format "%[fx:round(120*100)]" info:`
  • blurring="-morphology convolve blur:0x$sigma"
  • convert -size 35999x1 xc:black \
    \( -size ${full_range}x1 xc:white \) \
    -gravity center -compose over -composite \
    -roll -17998+0 -roll +${rollval}+0 \
    $blurring \
    $dir/lut.png
  • convert $infile \
    \( -clone 0 -define modulate:colorspace=$colorspace \
    -modulate $brightness,$saturation,$hue \) \
    \( -clone 0 -colorspace $colorspace -channel r -separate +channel \
    $dir/lut.png -interpolate nearest-neighbor -clut \) \
    -compose over -composite "$outfile"