Page 1 of 1

Can I improve my script ?

Posted: 2009-12-06T12:59:18-07:00
by fredmagick
Hi !!!

I played around with imagemagick cause I wanted to add a frame to all my flickr picyures.
The frame I wanted is a blur effect aroud my picture and a signature on the lower right-west area.
So here is my script which does the job, but as I'm new to imagemagick I was wondering if it can
be improved ?

Code: Select all

#!/usr/bin/env bash

# user variables
BORDER_COLOR=white
TEXT_COLOR=white
MAX_WIDTH=1600
BORDER_WIDTH=2

# auto compute value
SIZE_PX=$(($MAX_WIDTH/80))
TEXT_SIZE=$((MAX_WIDTH/50))
TEXT_OFF=$((TEXT_SIZE/5))

file=$1
outfile=$2

if [ ! -r $file ]; then
	echo "cannot read $file"
fi

echo -n "+ creating frame for $file"
convert $file -resize ${MAX_WIDTH}x${MAX_WIDTH} -auto-orient $outfile
WIDTH=$(identify -format "%w" $outfile)
HEIGHT=$(identify -format "%h" $outfile)

convert $outfile -resize $(($WIDTH+2*($SIZE_PX+$BORDER_WIDTH)))x$(($HEIGHT+2*($SIZE_PX+$BORDER_WIDTH)))! -blur 0x18 \( $outfile -bordercolor $BORDER_COLOR -border ${BORDER_WIDTH}x${BORDER_WIDTH} \) -gravity center -composite -strokewidth 1 -stroke none -fill $TEXT_COLOR -gravity southwest -pointsize $TEXT_SIZE -font "/home/nach/.fonts/Century.ttf" -annotate +$(($SIZE_PX+$BORDER_WIDTH+$TEXT_OFF))+$(($SIZE_PX+$BORDER_WIDTH)) "www.test.biz/photos" $outfile
echo "...done"
Best Regards
Fred

Re: Can I improve my script ?

Posted: 2009-12-06T15:32:21-07:00
by fmw42
you might get some improvement in speed by using an MPC file for a temp file (tmp.mpc and tmp.cache) then remove them upon running your script.

errMsg()
{
echo ""
echo $1
echo ""
exit 1
}

# set directory for temporary files
dir="/tmp" # suggestions are dir="." or dir="/tmp"

# setup temporary images
tmpA1="$dir/tmp_$$.mpc"
tmpA2="$dir/tmp_$$.cache"
trap "rm -f $tmpA1 $tmpA2; exit 0" 0
trap "rm -f $tmpA1 $tmpA2; exit 1" 1 2 3 15


# read the input image and test validity.
convert -quiet -regard-warnings "$infile" -resize ${MAX_WIDTH}x${MAX_WIDTH} -auto-orient +repage "$tmpA1" ||
errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO SIZE ---"

you can also add -ping to your identify commands to make them faster when just getting the width and height of an image.

you can also use -clone in your parenthesis processing

see http://www.imagemagick.org/Usage/files/#mpc and http://www.imagemagick.org/Usage/basics/#controls and http://www.imagemagick.org/Usage/basics/#clone

None of this is likely make any dramatic speed improvement

Re: Can I improve my script ?

Posted: 2009-12-07T03:47:05-07:00
by fredmagick
Hi back fmw42,

Thanks for those advices, I will have a look, you gave me some good ideas !
The goal was indeed to reduce the processing time, but moreover to check that I did think in the right way, cause imagemagick
is really flexible and you can to one thing in many way... like in Perl.

Thanks again
Best Regards.

Fred