Resizing Images for the web with ImageMagick

Over 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 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 800x800 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.

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

# currently not used but will enable the creation of thumbnails as well
# the square size that the photo should fit into in pixels
# the file type jpg/png/gifp
# 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.
# The watermark which 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

# resize_files by Chris Backhouse
# Published: Sept 2010
# Version: 0.2
# PURPOSE: to resize image files into a format
# ready for web use, apply a copyright notice and embed a watermark.
# 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.
echo `pwd`
if [ -f ".resize_files" ]; then
    echo "open Config File"
    source ./.resize_files
    while getopts 'r:f:t:c:' OPTION
        case $OPTION in
        c)    pCopyright="$OPTARG"
        d)    pCredit="$OPTARG"
        r)    iPhotoSize="$OPTARG"
        f)    pFileType="$OPTARG"
        t)    iThumbSize="$OPTARG"
        ?)    printf "Usage: %s: [-c Copyright-filename] [-d Credit-filename] [-r photosize] [-t thumbsize] [-f filetype jpg/png)] argsn" $(basename $0) >&2
            exit 2
    shift $(($OPTIND - 1))
if [ "$2" != "" ]; then
    SRC_DIR=`cd $2; pwd`
    echo "Source dir="$SRC_DIR
    echo "Warning: No source directory specified"
if [ "$1" != "" ]; then
    OUT_DIR=`cd $1; pwd`
    echo "Output dir="$OUT_DIR
    if [ ! -d $OUT_DIR ]; then
        echo "WARNING:  Cannot find Destination Directory - exiting"
cd "$OUT_DIR"
# Loop through files in current directory
# check if there is a smaller file in ./images
# and a thumbnail in ./thumbs
# if not then create
for filename in `find "$SRC_DIR" -iname "*.$pFileType"`; do
    #echo ">>Looping through files: $filename"
    fn=$(basename "$filename")
    echo ">>Checking: $fn"
    if [ -f "$filename" ]; then
    #echo "images/$fni.$pFileType"
        if [ ! -f "$fni.$pFileType" ] || [ "$fni.$pFileType" -ot "$filename" ] ; then
        if [ "$fni.$pFileType" -ot "$filename" ] ; then
            echo "  updating"
            echo "  converting"
        if [ "$pCopyright" = "" ] || [ "$pCredit" = "" ]; then
        #echo $tPS
        convert -resize $iPhotoSize "$filename" temp.$pFileType
        if [ "$pCopyright" != "" ]; then
            echo "  Adding copyright"
              composite -gravity center $pCopyright temp.$pFileType temp.$pFileType
        if [ "$pCredit" != "" ]; then
              echo "  Adding Credit"
              composite -gravity southeast $pCredit temp.$pFileType temp.$pFileType
        if [ "$pBorder" != "" ]; then
            convert -bordercolor #A0A0A0 -border $pBorder temp.$pFileType temp.$pFileType
        if [ "$pMontage" != "" ]; then
            echo     "  Squaring-up"
            squareup -s $pMontage -m pad -c "#A0A0A0" temp.$pFileType temp.$pFileType
        mv temp.$pFileType "$fni.$pFileType"
        echo "  output -> $iPhotoSize"
            iImage=$((iImage + 1))
echo "$iImage images processed"

Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...

Leave a Comment