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

ISOLATECOLOR


Isolates a particular color in an image.

Download Script

last modified: September 26, 2017



USAGE: isolatecolor [-c fcolor] [-C bcolor] [-t toler ] [-s fsat] [-S bsat ] infile outfile
USAGE: isolatecolor [-h or -help]

-c ... fcolor ... hue value or color from which to extract the hue
................. value for the isolated color (foreground); any IM
................. color value is allowed; hue values are 0<=hue<=360;
................. default=red
-C ... bcolor ... hue value or color from which to extract the hue
................. value for the rest of the image (background); any IM
................. color value is allowed; hue values are 0<=hue<=360;
................. default is background coloring as defined by bsat
-t ... toler .... hue tolerance (fuzz value) for foreground;
................. 0<=float<=50; default=10
-s ... fsat ..... saturation of foreground region; integer>=0;
................. default=100 (no change)
-S ... bsat ..... saturation of background region; integer>=0;
................. default=0 (grayscale)

PURPOSE: To isolate a particular color in an image.

DESCRIPTION: ISOLATECOLOR isolates a particular color in an image by thresholding on the corresponding hue value.

ARGUMENTS:

-c fcolor ... FCOLOR is the hue value or color from which to extract the hue value for the isolated color (foreground). Any IM color value is allowed. The hue values are 0<=hue<=360. The default=red.

-C bcolor ... BCOLOR is the hue value or color from which to extract the hue value for the rest of the image (background). Any IM color value is allowed. The hue values are 0<=hue<=360. The default is the background coloring as defined by bsat.

-t toler ... TOLER is the hue tolerance (fuzz value) for the foreground. Values are 0<=float<=50. The default=10.

-s fsat ... FSAT is the saturation of the foreground region. Values are integer>=0. The default=100 (no change).

-S bsat ... BSAT is the saturation of the background region. Values are integer>=0. The default=0 (grayscale). When background color is specified, a nominal value for bsat is about 25.

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


convert \( -size 151x151 xc:"rgb(100%,0%,0%)" -colorspace hsl \) \
\( -size 100x300 gradient: -rotate 90 +distort Polar '75,0,.5,.5' +repage -rotate 90 \) \
-compose CopyRed -composite -colorspace RGB \
\( -size 151x151 xc:black -fill white -draw "circle 75.5,75.5 75.5,151" -alpha off \) \
-compose copy_opacity -composite polar_rainbow_white.jpg


EXAMPLES


Example 1

Original Image
(source)

 

Arguments:
-c red -t 10 -S 0 -s 100
(defaults)


Example 2

Original Image
(source)

 

Arguments:
-c green1 -t 8 -S 0 -s 100

 

Arguments:
-c green1 -t 8 -S 0 -s 300

 

Arguments:
-c green1 -t 8 -C 30 -S 25 -s 100


Example 3

Original Image
(source)

 

Arguments:
-c blue -t 20 -S 0 -s 100

 

Arguments:
-c blue -t 20 -S 20 -s 100


What the script does is as follows:

  • read the input image to tempary mpc format
  • create 1D lut image that is white corresponding to the tolerance
    about the hue value and black elsewhere
  • clone the input image and desaturate it
  • clone the input image and saturate it
  • clone the input image and get the hue channel and apply the
    1D lut image to make a mask image
  • composite the desaturated image, the saturated image and the mask image
  • write the result as output

This is equivalent to the following IM commands

  • if [ "$hue" = "" ]; then
    hue=`convert xc:"$color" -colorspace HSL -format "%[pixel:u.p{0,0}]" info: | tr -cs "0-9." " " | cut -d\ -f2`
    else
    hue=`convert xc: -format "%[fx:100*$hue/360]" info:`
    fi
  • convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
  • len=`convert xc: -format "%[fx:2*($toler/100)*$lutlen]" info:`
  • len2=`convert xc: -format "%[fx:$len/2]" info:`
  • offset=`convert xc: -format "%[fx:($hue/100)*$lutlen-$len2]" info:`
  • convert -size ${lutlen}x1 xc:black -size ${len}x1 xc:white -compose over -composite -roll +${offset}+0 $tmpA2
  • convert $tmpA1 \
    \( -clone 0 -modulate 100,$desat,100 \) \
    \( -clone 0 -modulate 100,$sat,100 \) \
    \( -clone 0 -colorspace HSL -channel r -separate +channel $tmpA2 -clut \) \
    -delete 0 -compose over -composite $outfile