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

COLORATION


Mixes the color channels from an image into a single grayscale image using a weighted combination and then tints it

Download Script

last modified: September 26, 2017



USAGE: coloration [-h hue] [-s sat] [-l light] [-r red] [-g green] [-b blue]
[-B bright] [-C contrast] infile outfile [lutfile]

USAGE: coloration [-help]

-h ... hue ........ hue value to use for coloration; values are integers
................... either in range 0 to 360 (degrees) or 0 to 100 (percent)
................... depending upon the units argument; default=0 (red)
-s ... sat ........ saturation value to use for coloration; values are
................... integers in range 0 to 100 (percent); default=50
-l ... light ...... lightness value to use for coloration; values are
................... integers in range -100 to 100(percent); default=0
-u ... units ...... units for hue; choices are: degrees (d) or percent (p);
................... default=degrees
-r ... red ........ red mixing percent; float; default=29.9
-g ... green ...... green mixing percent; float; default=58.7
-b ... blue ....... blue mixing percent; float; default=11.4
-B ... bright ..... percent brightness change in coloration processing;
................... -100<=float<=100; default=10 (to match GIMP better)
-C ... contrast ... percent contrast change in coloration processing;
................... -100<=float<=100; default=-10 (to match GIMP better)

PURPOSE: To mix the color channels from an image into a grayscale image and tints it.

DESCRIPTION: COLORATION mixes the color channels from an image into a single grayscale image using a weighted combination and then tints it in a manner similar to GIMP Colorize and using argument values similar to those used by GIMP. The script generates a color look up table (lut) and applies it against the grayscale version of the image. The lut can be saved, if desired, for future use and will have a size of 256x20 pixels.

ARGUMENTS:

-h hue ... HUE value to use for coloration. The values are integers either in the range 0 to 360(degrees) or 0 to 100(percent) depending upon the units argument. The default=0 (red).

-s sat ... SAT is the saturation value to use for coloration. The values are integers in the range 0 to 100 (percent). The default=50.

-l light ... LIGHT is the lightness value to use for coloration. The values are integers in range -100 to 100(percent). The default=0 is the normal amount of lightness. Larger values will use IM colorize to mix pure white with the result. Smaller values will use IM colorize to mix pure black with the result.

-u units ... UNITS for specifying hue values. The choices are: raw (r) or percent (p). The default=raw

-r red ... RED is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=29.9 is equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=desat.

-g green ... GREEN is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=58.7 is equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=desat.

-b blue ... BLUE is the red mix percent (weight) in forming the grayscale image. Values are (pos. or neg.) floats. The default=11.4 is equivalent to IM non-linear grayscale. Nominally the r,g,b,o weights should total 100%. Weights are not relevant to form=desat.

-B bright ... BRIGHT is the percent change in brightness during processing. Values are floats in the range of -100 to 100. The default=10 (to match GIMP better).

-C contrast ... CONTRAST is the percent change in contrast during processing. image. Values are floats in the range of -100 to 100. The default=-10 (to match GIMP better).

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


Original Image

Hue \ Sat and Light

-s 50 -l 0

-s 25 -l 0

-s 75 -l 0

-s 50 -l -25

-s 50 -l 25

-h 0

-h 30

-h 60

-h 90

-h 120

-h 150

-h 180

-h 210

-h 240

-h 270

-h 300

-h 330



What the script does is as follows:

  • Creates a color look up table image as follows:
  • Convert R,G,B mix percents to fractions
  • Create lookup table (lut) image for coloration processing
  • Convert input image to grayscale according to the mixing fractions
  • Apply the lut to apply the color tint to the grayscale image
  • Combine some pure black or pure white to the image
    using -colorize according the amount of light specified
  • Applies the LUT to the image using -clut

This is equivalent to the following IM commands for units=degrees.

  • rf=`convert xc: -format "%[fx:$red/100]" info:`
  • gf=`convert xc: -format "%[fx:$green/100]" info:`
  • bf=`convert xc: -format "%[fx:$blue/100]" info:`
  • test=`convert xc: -format "%[fx:$light<0?0:1]" info:`
  • light=`convert xc: -format "%[fx:abs($light)]" info:`
  • if [ $test -eq 0 ]; then
    fillcolor="black"
    else
    fillcolor="white"
    fi
  • bright=`convert xc: -format "%[fx:$bright*128/100]" info:`
  • convert -size 1x1 xc:black xc:"hsl($hue%,$sat%,50%)" xc:white +append \
    -filter $filter -resize 256x1! \
    -set colorspace RGB -contrast-stretch 0 \
    -virtual-pixel edge -fx "u.p{(i-(w/2))*(100+$contrast)/(100)+(w/2)+$bright,j}" \
    $tmpL
  • convert $infile -color-matrix "\
    $rf $gf $bf \
    $rf $gf $bf \
    $rf $gf $bf \
    " \
    $tmpL -clut \
    -fill $fillcolor -colorize $light% \
    $outfile