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

MAGICWAND


Isolates a contiguous region of an image based upon a color determined from a user specified image coordinate.

Download Script

last modified: September 26, 2017



USAGE: magicwand x,y [-t threshold] [-f format] [-r region] [-m mask] [-c color] [-o opacity] infile outfile
USAGE: magicwand [-h or -help]

x,y .................. x,y location to get color and seed floodfill
-t .... threshold .... percent color disimilarity threshold (fuzz factor);
...................... values from 0 to 100; A value of 0 is an exact
...................... match and a value of 100 is any color; default=10
-f .... format ....... output format; image or mask; default=image
-r .... region ....... region to display; inside or outside; default=inside
-m .... mask ......... mask type; transparent, binary, edge, overlay, layer;
...................... default=transparent
-c .... color ........ color for background, edge outline or translucent layer;
...................... color="none" indicates use opacity;
...................... color="trans" indicates make background transparent;
...................... default="black"
-o .... opacity ...... opacity for transparent, overlay or layer mask;
...................... values from 0 to 100; default=0

PURPOSE: To isolate a contiguous region of an image based upon a color determined from a user specified image coordinate.

DESCRIPTION: MAGICWAND determines a contiguous region of an image based upon a color determined from a user specified image coordinate and a color similarity threshold value (fuzz factor). The output can be either an image or a mask. If the region is set to inside, then the image can be made to mask out the background as transparent, mask out the background using an opacity channel, fill the background with a color, display a boundary (outline) edge for the region or apply a translucent color layer over the background. If the region is set to outside, then the inside will be masked and the outside area will show normally. Alternately, the output can be a mask which is either binary (black and white), transparent (transparent and white) or a boundary edge (white on black background). The boundary edge can be made to match the interior or exterior depending upon the region setting.

ARGUMENTS:

x,y ... x,y are the coordinates in the image where the color is to be extracted and the floodfill is to be seeded.

-f format ... FORMAT specifies whether the output will be the modified input image or a mask image. The choices are image or mask. The default is image.

-r region ... REGION specifies whether the inside or outside are of the image will show and the other be masked out. The choices are inside or outside. The default is inside.

-m mask ... MASK specifies the type of mask to use or create. The choices are transparent, binary, edge, overlay or layer. Only transparent, binary or edge masks will be allowed as output. With a transparent mask, the image will be modified to mask out the complement of the regions specified according to the color setting. Specify the color setting to 1) "trans" to make the background transparent, 2) "none" to mask by multiplying by the opacity setting or 3) a color value to use a fill color. With an overlay mask, which will only be effective on PNG format output images, the masking is done via the opacity channel using the opacity setting. With a layer mask, a translucent color will be layered over the background according to the color and opacity values. The larger the opacity, the lighter the color overlay and the more the image will show.

-c color ... COLOR is the color to be used for the background fill or the boundary edge overlaid on the image. Any IM color specification is valid or a value of none. Be sure to enclose them in double quotes. The color value over-rides the opacity for mask=transparent, so use color="none" to allow the opacity to work or use color="trans" to make the background transparent. The default="black".

-o opacity ... OPACITY controls the degree of transparency for the area that is masked out using a mask setting of transparent, overlay or layer. A value of zero is fully transparent and a value of 100 is fully opaque.

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


Image Modification

Original Image

Arguments:
160,200 -t 25 -f image -r inside
-m edge -c red

Arguments:
160,200 -t 25 -f image -r outside
-c wheat

Arguments:
160,200 -t 25 -f image -r outside
-m transparent -c trans
(overlaid on checker image)

Arguments:
160,200 -t 25 -f image -r outside
-m transparent -c none -o 50

Arguments:
160,200 -t 25 -f image -r outside
-m overlay -o 50

Arguments:
160,200 -t 25 -f image -r inside
-m transparent -c none -o 50

Arguments:
160,200 -t 25 -f image -r inside
-m overlay -o 50
(overlaid on checkers)

Arguments:
160,200 -t 25 -f image -r inside
-m layer -c red -o 50

Arguments:
160,200 -t 25 -f image -r inside
-m transparent -c wheat

Arguments:
160,200 -t 25 -f image -r inside
-m transparent -c trans
(overlaid on checkers)

 

 


Masks

Original Image

Arguments:
160,200 -t 25 -f mask -r inside
-m binary

Arguments:
160,200 -t 25 -f mask -r outside
-m binary

Arguments:
160,200 -t 25 -f mask -r inside
-m transparent -c trans
(overlaid on checker image)

Arguments:
160,200 -t 25 -f mask -r outside
-m transparent -c trans
(overlaid on checker image)

Arguments:
160,200 -t 25 -f mask -r inside
-m edge



What the script does is as follows:

  • Creates a mask for the interior using the seed point to get
    the color and the color threshold value
  • Creates the negative, binary, or edge mask if needed
  • Applies the mask to the image

This is equivalent to the following IM commands for
the case of a replacement color of wheat for the region

  • convert $infile -fuzz 25% -fill none -draw "matte 160,200 floodfill" -fill black -colorize 100% $outfile
  • convert \( -size ${width}x${height} xc:white \) $outfile -composite -negate -transparent white $outfile
  • convert $infile $outfile -composite -fill wheat -opaque black $outfile