Can I improve my script ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
fredmagick

Can I improve my script ?

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Can I improve my script ?

Post 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
fredmagick

Re: Can I improve my script ?

Post 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
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 18 guests