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.

CONTOUR


Applies a contour outline to the image content.

Download Script

last modified: September 13, 2023



USAGE: contour [-t trim] [-T tolerance] [-d dilate] [-e erode] [-p pad] [-i icolor] [-o ocolor] [-s shadow] [-r ramping] [-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
.................... opaque color; default=white
-s ... shadow ...... shadow brightness in percent; 0<=integer<=100;
.................... default=30
-r ... ramping ..... shadow ramping amount (blur sigma value); integer>=0;
.................... default=10
-h ... highlight ... show black highlight edge; on or off; default=on
-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%
-v ... view ........ view connected-components information; yes or no; default=no

PURPOSE: To apply a contour outline to the image content.

DESCRIPTION: CONTOUR 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.

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 opaque color is allowed. 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=10.

-h highlight ... HIGHLIGHT is a flag to show a black highlight edge around the contour. Values are on or off. The default=on.

-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%.

-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 - Extract Mask By Floodfill Of White Background

Original Image

Arguments:
-a 0

Arguments:
-h off -a 0

Arguments:
-o pink -a 0



Example 2 - Extract Mask From Alpha Channel

Original Image
(source)

Arguments:
-a 0

Arguments:
-o pink -a 0



Example 3 - Mask Image Provided

Original Image (source)

Mask Image (source)

Arguments:
-a 0



Example 4 - Extract Mask By Floodfill Black Background

Original Image
(source)

Arguments:
-i black -a 0



Example 5 - Extract Mask By Floodfill White Background

Original Image

Arguments:
-d 35 -a 0

Arguments:
-d 10 -r 3 -a 0

Arguments:
-d 40 -e 30 -r 3 -a 0

Arguments:
-d 20 -r 0 -h off -a 2500



Example 6 - Extract Mask By Floodfill White Background

Original Image

Arguments:
-d 35 -f 20 -a 0



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
  • Dilates, inverts and blurs the mask to add the shadow
  • Composites the images together
  • Writes the output

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

  • convert \
    \( "$infile" -bordercolor "$icolor" -border 1 \
    -alpha off -fuzz $fuzzval% -fill none \
    -draw "matte $coords floodfill" \
    -fuzz $tolerance -trim +repage \
    -bordercolor none -border 50 \
    -background white -alpha background \
    -alpha off -write mpr:img -alpha on \
    -alpha extract -morphology dilate disk:$distance \
    -blur 0x1 -level 0x50% -write mpr:msk1 +delete \) \
    \( mpr:msk1 -negate -fill "gray($shadow%)" -opaque black \
    -fill "$ocolor" -opaque white \
    -blur 0x$ramping -write mpr:msk2 +delete \) \
    \( mpr:msk1 -morphology edgein diamond:1 -negate -write mpr:edg +delete \) \
    mpr:img mpr:msk1 -alpha off -compose copy_opacity -composite \
    mpr:msk2 -reverse -compose over -composite \
    mpr:edg -compose multiply -composite \
    "$outfile"