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.

CONTOUR2


Applies a directed contour to a transparent image.

Download Script

last modified: April 18, 2026



USAGE: contour [-t trim] [-T tolerance] [-d dilate] [-e erode] [-p pad] [-i icolor] [-o ocolor] [-s shadow] [-r ramping] [-x xamount] [-y yamount] [-h highlight] [-m method] [-f fuzzval] [-c coords] [-a area] [-v view] infile [maskfile] outfile USAGE: contour [-help]

-t ... trim ........ preprocess trim to image content bounding box;
.................... yes or no; default=yes
-T ... tolerance ... preprocessing trim tolerance percent (fuzz value);
.................... 0<=integer<=100; default=2
-d ... dilate ...... contour dilate distance in pixels; integer>=0; default=20
-e ... erode ....... contour erode distance in pixels; integer>=0; default=20
-p ... pad ......... image pad after trimming; integer>=0; default is twice
.................... the contour distance to allow room for the shadow
-i ... icolor ...... input image existing background color, which should be
.................... nearly a constant opaque color; used only if the input
.................... image is opaque and no mask image is provided;
.................... default=white
-o ... ocolor ...... output image desired background color; any IM allowed
.................... color; default=white; if none and input is transparent
.................... then a southeast directed shadow will be produced
-s ... shadow ...... shadow brightness in percent; 0<=integer<=100;
.................... default=30
-r ... ramping ..... shadow ramping amount (blur sigma value); integer>=0;
.................... default=5
-x ... xamount ..... amount of shadow in the x direction; integer>=0; default=5
.................... (east direction); west would be negative by adding "m" to
.................... argument value beginning; only applicable to transparent
.................... images; for xamount=yamount=positive the shadow will be
.................... directed southeast
-y ... yamount ..... amount of shadow in the y direction; integer>=0; default=5
.................... (south direction); north would be negative by adding "m" to
.................... argument value beginning; only applicable to transparent
.................... images; for xamount=yamount=positive, the shadow will be
.................... directed southeast
-h ... highlight ... show black highlight edge; edge thickness is integer>=0
.................... (morphology diamond size); default=1; 0 means no edge
-m ... method ...... method of generating transparency when the input image
.................... has no alpha channel and there is no mask; choices are:
.................... floodfill and recolor; floodfill will not leave holes
.................... in the result; recolor may leave holes in the result, if
.................... there are content gaps larger than the contour distance
.................... and these are desired; default=floodfill
-f ... fuzzval ..... fuzz value for the floodfill; 0<=integer<=100;
.................... default=2
-c ... coords ...... coordinates to start the floodfill; comma separate pair
.................... of x,y values; default=0,0 (upper left corner of the
.................... input image)
-a ... area ........ area of gaps to fill with connected-components; either
.................... integer pixels>=0 or integer percents>=0; default=1%
.................... 0 means no connected-components processing
-v ... view ........ view connected-components information; yes or no; default=no

PURPOSE: To apply a directed contour to a transparent image.

DESCRIPTION: CONTOUR2 applies a contour outline to the image content. This is generate via a mask image. The mask image may come from the alpha channel of the input image, from a mask image or by a transparent floodfill or transparent recoloring operation. The background color may be selected along with the shadowing and highlight edge around the contour. For opaque images, the shadow will all around. But for transparent images, the shadow will be directed (by default to the southeast).

ARGUMENTS:

-t trim ... TRIM is the preprocess trim to image content bounding box. Choices are: yes (y) or no (n). The default=yes.

-T tolerance ... TOLERANCE is the preprocessing trim tolerance percent (i.e., fuzz value). Values are 0<=integer<=100. The default=2.

-d dilate ... DILATE distance is the contour dilate distance in pixels. Values are integer>=0. The default=20. The actual distance will be the difference between the dilate distance and the erode distance. Larger values for dilate help fill gaps.

-e erode ... ERODE distance is the contour erode distance in pixels. Values are integer>=0. The default=0. The actual distance will be the difference between the dilate distance and the erode distance.

-p pad ... PAD is the image pad amount after trimming. Value are integer>=0. The default is twice the contour distance to allow room for the shadow.

-i icolor ... ICOLOR is the input image existing background color, which should be nearly a constant opaque color. This option is used only if the input image is opaque and no mask image is provided. The default=white.

-o ocolor ... OCOLOR is the output image desired background color. Any IM color is allowed including "none" when preserving input transparency. The default=white.

-s shadow ... SHADOW brightness in percent. Values are 0<=integer<=100. The default=30.

-r ramping ... RAMPING is the shadow ramping amount (blur sigma value). Values are integer>=0. The default=5.

-x xamount ... XAMOUNT is the amount of shadow in the x direction (right). Values are integers>=0. The default=5 (east direction). The west direction would be negative indicated by adding "m" to the argument value beginning. This argument is only applicable for transparent images. For xamount=yamount=positive, the shadow will be directed southeast.

-y yamount ... YAMOUNT is the amount of shadow in the y direction (down). Values are integers>=0. The default=5 (south direction). The north direction would be negative indicated by adding "m" to the argument value beginning. This argument is only applicable to transparent images. For xamount=yamount=positive, the shadow will be directed southeast.

-h highlight ... HIGHLIGHT shows a black highlight edge around the contour. Edge thickness has values that are integers>=0 (morphology diamond size). The default=1. 0 means no edge.

-f fuzzval ... FUZZVAL is the fuzz value for the floodfill. Value are 0<=integer<=100. The default=2.

-m method ... METHOD of generating transparency when the input image has no alpha channel and there is no mask. Choices are: floodfill (f) and recolor (r). A choice of floodfill will not leave holes in the result. A choice of recolor may leave holes in the result, if there are content gaps larger than the contour distance and these are desired. The default=floodfill.

-c coords ... COORDS are the coordinates to start the floodfill. They must be a comma separate pair of x,y values. The default=0,0 (upper left corner of the input image).

-a area ... AREA of gaps to fill with connected-components. Values are integer pixels counts>=0 or integer percents>=0. The default=1%. 0 means no connected-components processing.

-v view ... VIEW connected-components information. Choices are: yes or no. The default=no.

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 - Transparent Image (directed shadow)

Original Image
(source)

Arguments:
-a 0 -d 5 -s 0 -r 5 -x 5 -y 5 -h 1 -o none



Example 2 - Opaque Image (outline shadow)

Original Image

Arguments:
-a 0 -d 5 -e 0 -s 0 -r 5 -x 5 -y 5 -h 1 -o white



What the script does for m=morphology is as follows:

  • Reads the input
  • Extracts a mask either from the alpha channel, a separately provided mask, or by floodfill/recoloring
  • Copies the mask and makes and edge image
  • Creates a copy and applies -shadow to it
  • Then merges it with the original
  • Writes the output

This is equivalent to the following IM commands when the image has no alpha channel or no mask.

  • shadow=$((100-shadow))
  • convert \
  • \( $tmpA1 $preproc1 \
  • $trimming \
  • -bordercolor none -border $borderamt2 \
  • -background white -alpha background \
  • $alpha_disable -write mpr:img $alpha_enable \
  • -alpha extract \
  • -morphology dilate disk:$dilate \
  • -bordercolor black -border 1 \
  • $eroding \
  • -shave 1x1 \
  • $cclproc \
  • -blur 0x1 -level 0x50% +write t1.png -write mpr:msk1 +delete \) \
  • \( mpr:msk1 -morphology edgein diamond:$highlight -negate +write t2.png -write mpr:edg +delete \) \
  • mpr:img $highlighting mpr:msk1 -alpha off -compose copy_opacity -composite -compose over +write t3.png \
  • \( +clone -background black -shadow "${shadow}x${ramping}+${xweight}+${yweight}" \) \
  • +swap -background "$ocolor" -layers merge +repage \
  • "$outfile"