#!/bin/sh # # segment_image input_image multi_output_image # # Image of solid color areas (segments) seperate the image into a sequence # of images, with each image containing one unconnected color segment on a # transparent background. # # For more details see ImageMagcik Forum Discussion... # http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=11705 # # Anthony Thyssen 18 July 2008 # PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path PROGDIR=`dirname $PROGNAME` # extract directory of program PROGNAME=`basename $PROGNAME` # base name of program tmpdir=/tmp/$PROGNAME.$$ mkdir $tmpdir if [ $? -ne 0 ]; then echo >&2 "$PROGNAME: Failed to create directory \"$tmpdir\" -- ABORTING" exit 1 fi trap "rm -rf $tmpdir; exit 0" 0 trap "rm -rf $tmpdir; exit 1" 1 2 3 15 fuzz='0%' # fuzz factor for flood filling sepearte colored areas # This may be needed for JPEG image input. # ---------------------- # Read input image, once only as it may be pipelines, and save a MPC copy of # each individual row of pixels as temporary files. convert "$1" -matte $tmpdir/working.mpc while true; do # Find the first non-transparent pixel in inpit image. coords=`convert $tmpdir/working.mpc txt:- | sed '1d; / 0)/d; s/:.*//; q'` # If image is now only transparent, all segments found [ -z "$coords" ] && break echo >&2 "Extracting segment at coord $coords..." # Write the image BEFORE the segment is removed to the piepline... # Remove that segment from input image (mask) # Write the result back into the working image... convert $tmpdir/working.mpc -write miff:- \ -fuzz $fuzz -fill none -draw "matte $coords floodfill" \ $tmpdir/working.mpc done | # # Reverse image list so 'segments' are 'added' to each consecutive # image, then use 'OptimizeTransparency' to leave just the pixels that # were added to the image. Reverse and output to the multi-image file. # convert miff:- -reverse -layers OptimizeTransparency -reverse "$2"