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.

REPLACECOLOR


Changes one color to another color in an image.

Download Script

last modified: September 13, 2023



USAGE: replacecolor [-i icolor] [-o ocolor] [-f fuzzval ] [-g gain] [-t thresh] [-b brightness] [-s saturation] [-c colormode] infile [outdir]
USAGE: replacecolor [-help]

-i ... icolor ....... color in the input image to be replaced;
..................... any valid opaque IM color; default=blue
-o ... ocolor ....... color in the output image to replace the input color;
..................... any valid opaque IM color; default=red
-f ... fuzzval ...... fuzzval on each side of icolor for the range of hues;
..................... 0<=float<=180 degrees; default=40 degrees
-g ... gain ......... gain on color conversion; integers>=0; default=100
-t ... thresh ....... threshold value in percent for forcing low saturation
..................... colors to zero saturation, i.e. converts near gray
..................... (white through black) to pure gray; float>=0; default=0
-b ... brightness ... percent change in newcolor brightness; integer>=-100;
..................... default=0 (no change)
-s ... saturation ... percent change in new color saturation; 0<=integer<=100;
..................... default=0 (no change)
-c ... colormode .... colormode in which to do processing; choices are:
..................... hsv, hsl, hsi or hcl; default=hsv

PURPOSE: To change one color to another color in an image.

DESCRIPTION: REPLACECOLOR changes one color to another color in an image by modifying the input color hue to the desired output color hue and adjusting the saturation and brightness.

ARGUMENTS:

-i icolor ... ICOLOR is the color in the input image to be changed. Any valid opaque IM color is allowed. The default=blue

-o ocolor ... OCOLOR is the replacement color for the output image. Any valid opaque IM color is allowed. The default=red

-f fuzzval ... FUZZVAL on each side of the old color for the range of input hues to be recolored. Values are 0<=float<=180 degrees. The default=40.

-g gain ... GAIN on color conversion. Values are integers>=0. The default=100.

-t thresh ... THRESHOLD value in percent for forcing low saturation colors to zero saturation, i.e. converts near gray (white through black) to pure gray. Values are floats>=0. The default=0.

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

-s saturation ... SATURATION is the percent additional change in saturation. Values are -100<=integer<=100. The default=0 (no change).

-c colormode ... COLORMODE in which to do processing. The choices are: hsv, hsl, hsi or hcl. The default=hsv.

REQUIREMENTS: IM 6.5.3-7 or higher, so that -modulate uses HSL and not HSB colorspace.

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 -- Variation in Fuzz

Original

Arguments:
-i blue -o red -f 10 -g 100

Arguments:
-i blue -o red -f 20 -g 100

Arguments:
-i blue -o red -f 40 -g 100



Example 2 -- Variation in Gain

Original

Arguments:
-i "#39AC35" -o red -f 40 -g 50

Arguments:
-i "#39AC35" -o red -f 40 -g 100

Arguments:
-i "#39AC35" -o red -f 40 -g 400



Example 3 -- Variation in Color

Original
(source)

Arguments:
-i "#65D022" -o red -f 40 -g 100

Arguments:
-i "#65D022" -o blue -f 40 -g 100



What the script does is as follows for border=black at the top/bottom edges:

  • Convert input color to hsl in %
  • Convert output color to hsl in %
  • Read input image
  • Separate hsl channels
  • Make constant image of output hue
  • Multiply saturation channel by desired amount
  • Multiply brightness channel by desired amount
  • Combine modified hsl channels from previous lines and convert back to rgb
  • Create binary hue mask for desired fuzz values
  • Modify sat channel to threshold to 0 low saturation values and
    to apply gain to other saturation values as saturation mask
  • Composite last two images (hue mask and enhanced saturation mask image)
  • Delete temporary images
  • Composite original, modified rgb image and mask image
  • Write output

This is equivalent to the following IM commands

  • ihsl=`convert xc:"$icolor" -colorspace hsl txt: |\
    tail -n +2 | sed 's/^[ ]*//' | sed 's/[ ][ ]*/ /g' |\
    cut -d\ -f4 | sed -n 's/^.*[(]\(.*\)[)]$/\1/p'`
  • ihue=`echo "$ihsl" | cut -d, -f1`
  • isat=`echo "$ihsl" | cut -d, -f2`
  • ibri=`echo "$ihsl" | cut -d, -f3`
  • ihue=`echo "scale=6; 360*$ihue/100" | bc`
  • isat=`echo "scale=6; 100*$isat/255" | bc`
  • ibri=`echo "scale=6; 100*$ibri/255" | bc`
  • ohsl=`convert xc:"$ocolor" -colorspace hsl txt: |\
    tail -n +2 | sed 's/^[ ]*//' | sed 's/[ ][ ]*/ /g' |\
    cut -d\ -f4 | sed -n 's/^.*[(]\(.*\)[)]$/\1/p'`
  • ohue=`echo "$ohsl" | cut -d, -f1`
  • osat=`echo "$ohsl" | cut -d, -f2`
  • obri=`echo "$ohsl" | cut -d, -f3`
  • ohue=`echo "scale=6; 100*$ohue/360" | bc`
  • osat=`echo "scale=6; 100*$osat/255" | bc`
  • obri=`echo "scale=6; 100*$obri/255" | bc`
  • bri=`echo "scale=6; (50+$obri+$brightness)/100" | bc`
  • sat=`echo "scale=6; ($osat+$saturation)/100" | bc`
  • [ `echo "$bri < 0.0" | bc` -eq 1 ] && bri=0
  • [ `echo "$sat < 0.0" | bc` -eq 1 ] && sat=0
  • full_range=`convert xc: -format "%[fx:2*round($fuzzval*100)-1]" info:`
  • rollval=`convert xc: -format "%[fx:round($ihue*100)]" info:`
  • convert -size 35999x1 xc:black \
    \( -size ${full_range}x1 xc:white \) \
    -gravity center -compose over -composite \
    -roll -17998+0 -roll +${rollval}+0 \
    $dir/hlut.png
  • gain=$((100+gain))
  • convert \( $dir/tmpI.mpc -depth 8 \) \
    \( -clone 0 -colorspace hsl -separate +channel \) \
    \( -clone 1 -fill "gray($ohue%)" -colorize 100 \) \
    \( -clone 2 -evaluate multiply $sat \) \
    \( -clone 3 -evaluate multiply $bri \) \
    \( -clone 4,5,6 -set colorspace hsl -combine -colorspace sRGB \) \
    \( -clone 1 $dir/hlut.png -interpolate bilinear -clut \) \
    \( -clone 2 -black-threshold ${thresh}% +level 0x$gain% \) \
    \( -clone 8 -clone 9 -compose over -compose multiply -composite \) \
    -delete 1-6,8,9 \
    \( -clone 0 -clone 1 -clone 2 -compose over -composite \) \
    -delete 0-2 \
    "$outfile"