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.

UNSATURATEHUE


Desaturates a specific hue (range) in an image.

Download Script

last modified: August 17, 2020



USAGE: unsaturatehue [-h hue] [-u units] [-s saturation] [-r range] [-t taper] [-c colorspace] infile outfile
USAGE: unsaturatehue [-help]

-h ... hue .......... hue value to desaturate; choices are 0<integer<360
..................... if units=degrees or 0<integer<100 if units=percent;
..................... default=0 (red)
-u ... units ........ units for hue; percent (p) or degrees (d);
..................... default=degrees
-s ... saturation ... percent saturation; integer>=0; default=0
-t ... tolerance .... tolerance (range) around specified hue to desaturate
..................... as specified by units; 0<=integer<=360 degrees or
..................... 0<=integer<=100 percent; default=30 degrees
-r ... ramping ...... ramping (tapering) off graduation beyond tolerance;
..................... as specified by units; integer>=0; default=15 degrees
-c ... colorspace ... colorspace in which to desaturate the image;
..................... choices are: HSL or HCL; default=HSL

PURPOSE: To desaturate a given hue (range) in an image.

DESCRIPTION: UNSATURATEHUE desaturate a given hue (range) in an image. Hues are arranged in a cyclical order in the range 0 to 100 percent or 0 to 360 degrees. Hues may be desaturated to any amount between 0 and 100 percent. Units for hues may be either percent or degrees. Desaturation can be done in HSL or HCL colorspace.

ARGUMENTS:

-h hue ... Hue value to desaturate. The choices are 0

-u units ... UNITS for hue. Choices are percent (p) or degrees (d). The same units will be used for hue, tolerance and ramp. The default=degrees.

-s saturation ... SATURATION percent. Values are integer>=0. The default=0.

-t tolerance ... TOLERANCE (range) around specified hue to desaturate as specified by units. Values are 0<=integer<=360 degrees or 0<=integer<=100 percent. The default=30 (degrees).

-r ramping ... RAMPING (tapering) off graduation beyond tolerance as specified by units. Values are integer>=0. The default=15 (degrees).

-c colorspace ... COLORSPACE in which to desaturate the image. The choices are: HSL or HCL. The default=HSL.

REQUIREMENTS: IM 6.5.3-7 so that -modulate uses HSL and not HSB colorspace. HCL 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.


HUE REFERENCE CHART


EXAMPLES


Example 1

Original Image

Arguments:
-h 0 -s 0 -u d -t 30 -r 15 -c hsl
(desaturate red)


Example 2

Original Image

Arguments:
-h 64 -s 0 -u p -t 2 -r 2 -c hsl
(desaturate background)


What the script does is as follows:

  • Desaturates a copy of the image
  • Creates a (ramped) mask image from the hue channel
  • Composites the original and desaturated image together using the mask image

This is equivalent to the following IM commands for the case of units=percent.

  • addval=`convert xc: -format "%[fx:50-$hue]" info:`
  • low=`convert xc: -format "%[fx:100-$tolerance-2*$ramping]" info:`
  • high=`convert xc: -format "%[fx:100-$tolerance]" info:`
  • convert "$infile" \
    \( -clone 0 -define modulate:colorspace=$colorspace -modulate 100,$saturation,100 \) \
    \( -clone 0 -colorspace $colorspace -channel 0 -separate +channel \
    -evaluate AddModulus ${addval}% \
    -solarize 50% -level 0x50% \
    -level "${low}%,${high}%" \) \
    -compose Over -composite \
    "$outfile"