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

PUZZLE


Applies a puzzle like effect to an image.

Download Script

last modified: June 08, 2015



USAGE: puzzle [-s scale] [-o ocolor] [-e ecolor] [-m missing] [-n newseed] [-r rotation] [-x xoffset] [-y yoffset] [-O opacity] [-D depth] [-length] infile outfile
USAGE: puzzle [-h or -help]

-s ... scale ..... puzzle pieces scale factor; integer>=50; default=100
-o ... ocolor .... puzzle outline color; any valid IM color or "carve";
.................. default=gray40; default=gray40
-e ... ecolor .... puzzle empty (missing) piece color; any valid IM color;
.................. default=white
-m ... missing ... number of missing (empty) pieces; 0<=integer<=20; default=0
-n ... newseed ... seed for random missing cells; integer; default=no seed
-r ... rotate .... rotation of overlaid missing pieces; -360<=integer<=360;
.................. default=0
-x ... xoffset ... x offset of overlaid missing pieces; integer>=0; default=0
-y ... yoffset ... y offset of overlaid missing pieces; integer>=0; default=0
-O ... opacity ... opacity for shadow effect on overlaid missing pieces;
.................. 0<=integer<=100; default=0
-D ... depth ..... depth for shadow effect on overlaid missing pieces;
.................. integer>=0; default=3
-L ... length .... length of shadow on overlaid missing pieces; integer>=0;
.................. default=3

PURPOSE: To apply a puzzle like effect to an image.

DESCRIPTION: PUZZLE applies a puzzle like effect to an image. Options include: setting the number of missing pieces, setting the missing piece color and the outline color and overlaying the missing pieces at some rotation or offset.

ARGUMENTS:

-s scale ... SCALE is the puzzle cell scale factor. Values are integers>=50. The default=100. At scale of 100, the size of a cell is about 50x50 pixels.

-o ocolor ... OCOLOR is the puzzle piece outline color. Any valid IM color is allowed or the special value of "carve" (without the quotes). The default=gray40. Using carve will produce a carved-like effect to the puzzle. The default=gray40.

-e ecolor ... ECOLOR is the color used to fill empty (missing) pieces. Any valid IM color is allowed. The default=white.

-m missing ... MISSING is the number of missing (empty) pieces. Values are 0<=integer<=20. The default=0.

-n newseed ... NEWSEED is the seed for random missing pieces. Values are non-negative integers. The default is no seed. Thus the pattern will not be repeatable.

-r rotate ... ROTATE is the rotation angle of overlaid missing pieces. Values are -360<=integer<=360. The default=0. Pieces will not be overlaid unless at least one of rotate, xoffset, yoffset are non-zero.

-x xoffset ... XOFFSET is the x offset of overlaid missing pieces. Value are integers (positive or negative). The default=0. Pieces will not be overlaid unless at least one of rotate, xoffset, yoffset are non-zero.

-y yoffset ... YOFFSET is the y offset of overlaid missing pieces. Value are integers (positive or negative). The default=0. Pieces will not be overlaid unless at least one of rotate, xoffset, yoffset are non-zero.

-O opacity ... OPACITY for shadow effect on overlaid missing pieces. Values are 0<=integer<=100. The default=0.

-D depth ... DEPTH for shadow effect on overlaid missing pieces. Values are integer>=0. The default=3.

-L length ... LENGTH of shadow on overlaid missing pieces. Values are integer>=0. The default=3.

Requirements: IM 6.5.9.3 or higher due to the use of 1D morphology edge.

References - svg puzzle pattern modified from: http://www.joshpodolske.com/puzzle-pieces-vector-swatch/

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

Original

Arguments:
-s 100 -o gray40 -e white -m 0 -n 1 -O 0

Arguments:
-s 80 -o gray40 -e white -m 3 -n 1 -O 0

Arguments:
-s 100 -o gray40 -e white -m 3 -n 1 -O 0

Arguments:
-s 120 -o gray40 -e white -m 3 -n 1 -O 0

Arguments:
-s 100 -o gray40 -e none -m 3 -n 1 -O 0
(overlaid on checker background image)

Arguments:
-s 100 -o gray40 -e white -m 3 -n 1 -r 5 -x 20 -y 0 -O 0

Arguments:
-s 100 -o gray40 -e white -m 3 -n 1 -r 5 -x 20 -y 0 -O 80



Example 2

Original

Arguments:
-s 100 -o carve -e white -m 0 -n 1 -O 0

Arguments:
-s 100 -o carve -e white -m 3 -n 4 -O 0

Arguments:
-s 100 -o carve -e white -m 3 -n 4 -r 5 -x 10 -y 0 -O 80



What the script does is as follows:

  • Scales and tiles an svg puzzle cell to fill the size of the input image
  • Takes a copy of the tiled puzzle outline mask and composites over the input with ocolor
  • Takes a copy of the tiled puzzle outline mask and fills random cells with transparency for the
    missing pieces, then converts that to binary and places that in the alpha channel of the previous
    composited image then flatten it with the ecolor
  • Takes the original image and puts the binary image into the alpha channel to select only the missing
    pieces then rotates and rolls the image by the offsets. Then creates an edge image from the binarized
    previous image and composites it with the rotated/offset image with ocolor to add the outline.
    Then adds shadows and composites it over the previous result

This is equivalent to the following IM commands

  • function randomPts()
    {
    seed=$1
    if [ "$seed" = "" ]; then
    seedingx=""
    seedingy=""
    else
    seedx=$((seed+1))
    seedy=$((seed+2))
    seedingx="-seed $seedx"
    seedingy="-seed $seedy"
    fi
    xx=`convert xc: $seedingx -format "%[fx:round($ww1*random())]" info:`
    yy=`convert xc: $seedingy -format "%[fx:round($hh1*random())]" info:`
    }
  • convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
  • ww=`convert $tmpA1 -format "%w" info:`
  • hh=`convert $tmpA1 -format "%h" info:`
  • ww1=$((ww-1))
  • hh1=$((hh-1))
  • convert -density $density $tmpS -write mpr:tile +delete -size ${ww}x${hh} tile:mpr:tile +write $tmpA3 $tmpA2
  • if [ $missing -gt 0 ]; then
    drawing="-draw"
    n=1
    seed=$newseed
    until [ $n -gt $missing ]; do
    # get random x,y
    randomPts "$seed"
    # test if color equals pure black
    testcolor=`convert $tmpA3 -depth 8 -format "%[pixel:u.p{$xx,$yy}]" info:`
    if [ "$testcolor" = "black" ]; then
    args="matte $xx,$yy floodfill"
    convert $tmpA3 -fuzz 50% -fill none $drawing "$args" $tmpA3
    n=$((n+1))
    fi
    seed=$((seed+1))
    done
    fi
  • cx=`convert $tmpA1 -format "%[fx:w/2]" info:`
  • cy=`convert $tmpA1 -format "%[fx:h/2]" info:`
  • x1=`convert xc: -format "%[fx:$cx+$xoffset]" info:`
  • y1=`convert xc: -format "%[fx:$cy+$yoffset]" info:`
  • convert $tmpA1 \
    \( -clone 0 -fill "$ocolor" -colorize 100 \) \
    $tmpA2 -compose over -composite \
    \( $tmpA3 -fill "rgba(255,255,255,1)" +opaque "rgba(0,0,0,0)" -write mpr:missing \) \
    -alpha off -compose copy_opacity -composite \
    -compose over -background "$ecolor" -compose over -flatten \
    \( \( $tmpA1 \( mpr:missing -alpha off -negate \) -compose copy_opacity -composite \
    -background none -virtual-pixel none -filter point -distort SRT "$cx,$cy 1 $rotate $x1,$y1" \) \
    \( +clone -fill white +opaque none -alpha off -morphology edge diamond:1 \) \
    \( +clone -fill "$ocolor" -colorize 100 \) +swap -compose over -composite \
    \( +clone -background black -shadow ${opacity}x${depth}+${length}+${length} \) \
    +swap -background none -layers merge +repage \) \
    -compose over -flatten $outfile