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

CONTOUR


Applies a contour outline to the image content.

Download Script

last modified: April 04, 2017



USAGE: contour [-t trim] [-T tolerance] [-d distance] [-p pad] [-i icolor] [-o ocolor] [-s shadow] [-r ramping] [-h highlight] [-m method] [-f fuzzval] [-c coords] 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 ... distance .... contour 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)

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 distance ... DISTANCE is the contour distance in pixels. Values are integer>=0. The default=20.

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


EXAMPLES


Example 1 - Extract Mask By Floodfill Of White Background

Original Image

Arguments:
(defaults)

Arguments:
-h off

Arguments:
-o pink



Example 2 - Extract Mask From Alpha Channel

Original Image
(source)

Arguments:
(defaults)

Arguments:
-o pink



Example 3 - Mask Image Provided

Original Image (source)

Mask Image (source)

Arguments:
(defaults)



Example 4 - Extract Mask By Floodfill Black Background

Original Image
(source)

Arguments:
-i black



Example 5 - Extract Mask By Floodfill White Background

Original Image

Arguments:
-d 35

Arguments:
-d 10 -r 3



Example 6 - Extract Mask By Floodfill White Background

Original Image

Arguments:
-d 35 -f 20



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"