Fred's ImageMagick Scripts



    Licensing:

    My scripts are available free of charge for non-commercial use.

    If you redistribute or incorporate any of these scripts into other free applications, you may use my scripts by simply referencing my name and this web page: Fred Weinhaus and http://www.fmwconcepts.com/imagemagick/index.html

    For use of my scripts in commercial use or non-free applications, please contact me for licensing arrangements.
    My email address is fmw at alink dot net.

    Usage, whether stated in script or not, is also subject to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

TRICOLORIZE


Smoothly colorize a grayscale image with either one, two or three different colors

Download Script

last modified: June 30, 2011



USAGE: tricolorize [-l lcolor] [-m mcolor] [-h hcolor] [-c contrast] [-o offset] [-t type] infile outfile [lutfile]
USAGE: tricolorize [-l lcolor] [-m mcolor] [-h hcolor] [-c contrast] [-o offset] [-t type] lutfile
USAGE: tricolorize [-help]

-l .... lcolor ........... Color for low end of grayscale; Any valid IM color is allowed;
.......................... The default=black
-m .... mcolor ........... Color for mid range of grayscale; Any valid IM color is allowed;
.......................... The default=gray50
-h .... hcolor ........... Color for high end of grayscale; Any valid IM color is allowed;
.......................... The default=white
-c .... contrast ......... percent contrast change; integers; -100<=contrast<=100; default=0
-o .... offset ........... percent color offset; integers; -100<=offset<=100; default=0
-t .... type ............. Type of look up table (lut) smoothing/interpolation; choices are:
.......................... cubic, quadratic, gaussian, triangle, point; default=triangle

PURPOSE: To smoothly colorize a grayscale image with either one, two or three different colors.

DESCRIPTION: TRICOLORIZE smoothly colorizes a grayscale image with either one, two or three different colors. If the input image is color, it will be converted to grayscale first. Using an appropriate mid color with low color of black and high color of white will allow one to tint an image. This achieves the same result as my tricolorize script, except the default type there is quadratic. 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:

-l lcolor ... LCOLOR is the color to use at the low end of the grayscale. The default=black. Any valid IM color is allowed. See http://imagemagick.org/script/color.php

-m mcolor ... MCOLOR is the color to use in the mid grayscale range. The default=gray50. Any valid IM color is allowed. See http://imagemagick.org/script/color.php For tinting, a good choice of colors may be specified as hsl with saturation about 25% and lightness about 50% and your choice of hue in range 0 to 360 degrees. For reference, see http://homepages.cwi.nl/~steven/css/hsl-examples.html

-h hcolor ... HCOLOR is the color to use at the high end of the grayscale. The default=white. Any valid IM color is allowed. See http://imagemagick.org/script/color.php

-c contrast ... CONTRAST is the percent change in color contrast. Values are integers such that -100<=contrast<=100. The default=0.

-o offset ... OFFSET is the percent shift of the colors in the lut. Values are integers such that -100<=offset<=100. Positive values shift the colors towards the brighter end of the grayscale and negative values shift the colors towards the darker end of the grayscale. The default=0.

-t type ... TYPE of smoothing/interpolation of the colors to fill out the look up table. The choices are: cubic, gaussian, quadratic, triangle and point. The default=triangle.

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


Variations In 1 Color (Tinting)

 

Original Image

 

   

 

Grayscale Image

 

   

Arguments:

Tinted Image

Color LUT Image

-m "hsl(0,25%,50%)"

-m "hsl(30,25%,50%)"

-m "hsl(60,25%,50%)"

-m "hsl(90,25%,50%)"

-m "hsl(120,25%,50%)"

-m "hsl(150,25%,50%)"

-m "hsl(180,25%,50%)"

-m "hsl(210,25%,50%)"

-m "hsl(240,25%,50%)"

-m "hsl(270,25%,50%)"

-m "hsl(300,25%,50%)"

-m "hsl(330,25%,50%)"



2 Colors -- Blue, Yellow, Blue -- Variations In Type

 

Original Image

 

   

 

Grayscale Image

 

   

Arguments:

Colorized Image

Color LUT Image

-l blue -m yellow -h blue -t cubic

-l blue -m yellow -h blue -t quadratic

-l blue -m yellow -h blue -t gaussian

-l blue -m yellow -h blue -t triangle



3 Colors -- Blue, Yellow, Red -- Variations In Contrast

 

Original Image

 

   

 

Grayscale Image

 

   

Arguments:

Colorized Image

Color LUT Image

-l blue -m yellow -h red -c -20

-l blue -m yellow -h red -c 0

-l blue -m yellow -h red -c 20



3 Colors -- Blue, Yellow, Red -- Variations In Offset

 

Original Image

 

   

 

Grayscale Image

 

   

Arguments:

Colorized Image

Color LUT Image

-l blue -m yellow -h red -o -10

-l blue -m yellow -h red -o 0

-l blue -m yellow -h red -o 10



What the script does is as follows:

  • Creates a color look up table image as follows:
    • Appends 1x1 images of low color, mid color and high color
    • Resizes to 256x1 using -filter with the type specified
    • Contrast stretches it
    • Stretches/Compresses and Offsets the colors in the image
      according to the contrast and offset specified using -fx
  • Applies the LUT to the image using -clut

This is equivalent to the following IM commands for the luminance colormode.

  • offset=`convert xc: -format "%[fx:$offset*128/100]" info:`
  • convert -size 1x1 xc:$lcolor xc:$mcolor xc:$hcolor +append \
    -filter $type -resize 256x1! \
    -contrast-stretch 0 \
    -virtual-pixel edge -fx "u.p{(i-(w/2))*(100+$contrast)/(100)+(w/2)-$offset,j}" \
    $tmp0
  • convert $infile $tmp0 -clut $outfile