Resizing Images for the web with ImageMagick

iStock_000004589853MediumOver the years I have used imagemagick and convert on Linux machines to manipulate image files and be able to resize them ready for web use in batches.  Until recent times there have been few image manipulation tools that also enabled watermark embedding in batch mode.  Now all the main programs, whether windows, OSX or Linux based, have batch system that enable some variety of watermarking or crediting.

However, I still find that many of these tools, including the base offering in Lightroom, Photoshop and digiKam all have their deficiencies.

I have therefore kept going with my scripts and now that I have become a semi-professional photographer (see http://kiff-backhouse.com) I need to be able to easily change the Credit Line and the Watermark for each project or Client.

The following script has evolved and is STILL very much in evloution – please feel free to use it BUT with no guarantees. I would also love to hear your feedback and even improvements…

The script

So the system is in 2 parts.  A BASH script that loops through all the images of the given type within a directory and for each one:-

i) checks that it needs updating in the target directory

ii) Resizes the image to the required size.  you give it a square that you want the image to fit into, for example 800×800 pixels and whether the image is portrait or landscape the long side will be 800 pixels

iii) if you have chosen to include a watermark, this is added to the center of the image

iv) if you have chosen to add a Credit, this is added to the bottom right hand corner

v) if you have chosen to have a square image, it will mount the source rectangular image on a coloured background of your choice. Why: this makes it easier to format a table/divs of images in a galllery.  if you mount the image on a square card with the same colour background as the gallery, then formatting is much easier. See http://store.rockwaterstudio.com/Bracelets

vi) output to target directory

How to control the script

So as normal all these options can be controlled through command line options.  See below for details.  However, I more often can’t be bothered to type all these out (laziness is the mother of invention), so I use a control file which is stored in the current directory.  The way that I organise my images, they are often held in a Project or Client top level directory and therfore I can put a control file in each directory that suits the format that that client requires.

The control file is called .resize_files and has the following parameters

 PHP |  copy code |? 
1
iThumbSize=0 # currently not used but will enable the creation of thumbnails as well

 PHP |  copy code |? 
1
iPhotoSize=640 # the square size that the photo should fit into in pixels

 PHP |  copy code |? 
1
pFileType="jpg" # the file type jpg/png/gif

 PHP |  copy code |? 
1
pCredit=/share/data/Pictures/AL/credit.png # the credit line. for example "Photo by Kiff Backhouse".  At the moment this only accepts a graphic file, which should include transparency for everyhting else other than the credit.

 PHP |  copy code |? 
1
pCopyright=/share/data/Pictures/AL/copyright.png # The watermark whic is added to the center of the image

If any of these are not needed, just leave the right hand side blank

So here is the script source.  Just cut and paste it into a file, save and then check the permissions to make it executable.

You will also need to download a script called squareup from Fred Weinhaus

 PHP |  copy code |? 
1
#!/bin/bash<br />#<br /># resize_files by Chris Backhouse<br /># Published: Sept 2010<br /># Version: 0.2<br />#<br /># PURPOSE: to resize image files into a format<br /># ready for web use, apply a copyright notice and embed a watermark.<br />#<br /># CAVEAT: No guarantee that this script will work on all platforms,<br /># nor that trapping of inconsistent parameters is complete and<br /># foolproof. Use At Your Own Risk.<br />#<br />iThumbSize=0<br />iQuality=75<br />iImage=0<br />iThumbs=0<br />pFileType="jpg"<br />pTemp="temp"<br /><br />echo `pwd`<br /><br />if [ -f ".resize_files" ]; then<br />&nbsp;&nbsp;&nbsp; echo "open Config File"<br />&nbsp;&nbsp;&nbsp; source ./.resize_files<br />else<br />&nbsp;&nbsp;&nbsp; while getopts 'r:f:t:c:' OPTION<br />&nbsp;&nbsp;&nbsp; do<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;case $OPTION in<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;c)&nbsp;&nbsp; &nbsp;pCopyright="$OPTARG"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;d)&nbsp;&nbsp; &nbsp;pCredit="$OPTARG"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;r)&nbsp;&nbsp; &nbsp;iPhotoSize="$OPTARG"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;f)&nbsp;&nbsp; &nbsp;pFileType="$OPTARG"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;t)&nbsp;&nbsp; &nbsp;iThumbSize="$OPTARG"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;?)&nbsp;&nbsp; &nbsp;printf "Usage: %s: [-c Copyright-filename] [-d Credit-filename] [-r photosize] [-t thumbsize] [-f filetype jpg/png)] argsn" $(basename $0) &gt;&amp;2<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;exit 2<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;;;<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;esac<br />&nbsp;&nbsp;&nbsp; done<br />&nbsp;&nbsp;&nbsp; shift $(($OPTIND - 1))<br />fi<br /><br />SRC_DIR=`pwd`<br />if [ "$2" != "" ]; then<br />&nbsp;&nbsp;&nbsp; SRC_DIR=`cd $2; pwd`<br />&nbsp;&nbsp;&nbsp; echo "Source dir="$SRC_DIR<br />else<br />&nbsp;&nbsp;&nbsp; echo "Warning: No source directory specified"<br />&nbsp;&nbsp;&nbsp; exit<br />fi<br />cd "$START_DIR"<br />if [ "$1" != "" ]; then<br />&nbsp;&nbsp;&nbsp; OUT_DIR=`cd $1; pwd`<br />&nbsp;&nbsp;&nbsp; echo "Output dir="$OUT_DIR<br />&nbsp;&nbsp;&nbsp; if [ ! -d $OUT_DIR ]; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;echo "WARNING:&nbsp; Cannot find Destination Directory - exiting"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;exit<br />&nbsp;&nbsp;&nbsp; fi<br />fi<br /><br />cd "$OUT_DIR"<br /># Loop through files in current directory<br /># check if there is a smaller file in ./images<br /># and a thumbnail in ./thumbs<br /># if not then create<br /><br />for filename in `find "$SRC_DIR" -iname "*.$pFileType"`; do<br />&nbsp;&nbsp;&nbsp; #echo "&gt;&gt;Looping through files: $filename"<br />&nbsp;&nbsp;&nbsp; fn=$(basename "$filename")<br />&nbsp;&nbsp;&nbsp; fni=${fn%%.*}<br />&nbsp;&nbsp;&nbsp; echo "&gt;&gt;Checking: $fn"<br />&nbsp;&nbsp;&nbsp; if [ -f "$filename" ]; then<br />&nbsp;&nbsp;&nbsp; #echo "images/$fni.$pFileType"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if [ ! -f "$fni.$pFileType" ] || [ "$fni.$pFileType" -ot "$filename" ] ; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if [ "$fni.$pFileType" -ot "$filename" ] ; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo "&nbsp; updating"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;else<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo "&nbsp; converting"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;fi<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$pCopyright" = "" ] || [ "$pCredit" = "" ]; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;pTemp="$fni.$pFileType"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi<br /><br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #tPS="${iPhotoSize}x${iPhotoSize}"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #echo $tPS<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; convert -resize $iPhotoSize "$filename" temp.$pFileType<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$pCopyright" != "" ]; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo "&nbsp; Adding copyright"<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composite -gravity center $pCopyright temp.$pFileType temp.$pFileType<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$pCredit" != "" ]; then<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo "&nbsp; Adding Credit"<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composite -gravity southeast $pCredit temp.$pFileType temp.$pFileType<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$pBorder" != "" ]; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;convert -bordercolor #A0A0A0 -border $pBorder temp.$pFileType temp.$pFileType<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if [ "$pMontage" != "" ]; then<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;echo &nbsp;&nbsp; &nbsp;"&nbsp; Squaring-up"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;squareup -s $pMontage -m pad -c "#A0A0A0" temp.$pFileType temp.$pFileType<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; fi<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mv temp.$pFileType "$fni.$pFileType"<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; echo "&nbsp; output -&gt; $iPhotoSize"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iImage=$((iImage + 1))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fi<br />&nbsp;&nbsp;&nbsp; fi<br />done<br /><br />echo "$iImage images processed"<br /><br />