Fred's ImageMagick Scripts



 

 

CARTOON


Creates a cartoon-like appearance to an image.

Download Script

last modified: November 27, 2011



USAGE: cartoon [-m method] [-n numcolors] [-i imagefilter] [-e edgefilter] [-p pctedges] [-s smooth] infile outfile
USAGE: cartoon [-h or -help]

-m .... method .......... method of color reduction; choices are 1 or 2;
......................... default=1
-n .... numcolors ....... number of desired colors; integer>0; default=10
-i .... imagefilter ..... size of median filter to preprocess image;
......................... integer>=0; default=2
-e .... edgefilter ...... size of median filter to preprocess grayscale image
......................... before extracting gradient edges; integer>=0; default=2
-p .... pctedges ........ percentage of edges to use from gradient images;
......................... 0<=integer<=100; default=75
-s .... smooth .......... size of blur filter to smooth the image before
......................... applying the edges; float>=0

PURPOSE: To create a cartoon-like appearance to an image.

DESCRIPTION: CARTOON creates a cartoon-like appearance to an image. The image is color reduced to try to achieve the desired number of colors. There are two methods to do this. The first is not terribly sensitive to the desired number of colors. Values in the range of about 6-14 generally produce the same result. But the image can be a bit noisy/grainy, so post smoothing is generally needed. The second produces different results for different desired number of colors and generally is not as noise sensitive. Edges can then be superimposed onto the image. The edges are produced by a gradient edge detector.

ARGUMENTS:

-m method ... METHOD is the color reduction method. The choices are 1 or 2. Method 1 uses the IM -colors function channel-by-channel. Method 2 computes a -fx formula and is applied via -clut. The default=1

-n numcolors ... NUMCOLORS is the desired number of reduced colors. Values are integers>0. The default=10.

-i imagefilter ... IMAGEFILTER is the size of a median filter that will be applied to the image before reducing colors. Values are integers>=0. The default=2.

-e edgefilter ... EDGEFILTER is the size of a median filter that will be applied to a grayscale version of the image before extracting edges. Values are integers>=0. The default=2.

-p pctedges ... PCTEDGES is the percentage of the edges from the gradient edge extraction from the grayscale image to overlay on the image. Values are 0<=integer<=100. The default=75.

-s smooth ... SMOOTH is the amount of (-blur) smoothing to apply to the color reduced image before the edges are overlayed. Values are floats>=0. The default=1.

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 1 -- Variation In Edge Filtering

Original

Arguments:
(defaults)

Arguments:
-e 1



Image 2 -- Variation In Method

Original

Arguments:
-m 1 -p 65

Arguments:
-m 2 -p 65



Image 2 -- Variation In Method 2 Number Of Colors

Arguments:
-m 2 -n 8 -s 0 -p 0

Arguments:
-m 2 -n 10 -s 0 -p 0

Arguments:
-m 2 -n 12 -s 0 -p 0



Image 3

Original

Arguments:
(defaults)



What the script does is as follows:

  • (Optionally) applies a median filter to the image
  • Reduces the number of colors in the filtered image
  • Converts the original image to grayscale
  • (Optionally) applies a median filter to the grayscale image
  • Applies a gradient edge detector to the grayscale image
  • Thresholds the edge image to binary
  • Composites the edge image with the color reduced image

This is equivalent to the following IM commands

  • dx="-1,0,1,-1,0,1,-1,0,1"
  • dy="1,1,1,0,0,0,-1,-1,-1"
  • convert $infile -median $imagefilter -separate \
    -colors $numcolors +dither \
    -combine -blur 0x$smooth $tmpA2
  • convert \( $infile -colorspace gray -median $imagefilter \) \
    \( -clone 0 -bias 50% -convolve "$dx" -solarize 50% \) \
    \( -clone 0 -bias 50% -convolve "$dy" -solarize 50% \) \
    \( -clone 1 -clone 1 -compose multiply -composite -gamma 2 \) \
    \( -clone 2 -clone 2 -compose multiply -composite -gamma 2 \) \
    -delete 0-2 -compose plus -composite -threshold ${pctedges}% $tmpA3
  • convert $tmpA2 $tmpA3 -compose multiply -composite $outfile