Resize and Add Caption to multiple images using bash script

The most loveliest feature of GNU/Linux is we can automate anything we do manually.

I capture the nice moments of the events I participate and will upload the pictures in photo sharing websites like picasa, facebook etc.

We should

  • filter – remove unwanted, bad looking photos
  • rotate – rotate them to view easily
  • resize – 640×480 is enough to view the photos online
  • add caption – A Caption about the event is good.

Filter and Rotation should be done manually.

Resize and Adding caption can be done manually using GIMP. But, imaging resizing and adding caption for all hundreds of images often will make the life boring. Soon, we will run out of photography.

Here, Shell script comes to help with its automation power tools.
The commandline image manipulation tool imagemagick gives a bunch of utilities to manipulate images.

Install it using

sudo apt-get install imagemagick

in ubuntu/debian machines.

It gives the following utilities.

animate
animate an image sequence on any X server.
compare
mathematically and visually annotate the difference between an image and its reconstruction.
composite
overlap one image over another.
conjure
interpret and execute scripts written in the Magick Scripting Language (MSL).
convert
convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.
display
display an image or image sequence on any X server.
identify
describe the format and characteristics of one or more image files.
import
save any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
mogrify
resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. Mogrify overwrites the original image file, whereas, convert writes to a different image file.
montage
create a composite image by combining several separate images. The images are tiled on the composite image optionally adorned with a border, frame, image name, and more.
stream
a lightweight tool to stream one or more pixel components of the image or portion of the image to your choice of storage formats. It writes the pixel components as they are read from the input image a row at a time making stream desirable when working with large images or when you require raw pixel components.

Find the usage here:
http://www.imagemagick.org/Usage/

Here is the script I use to automate the process of resizing and adding caption to Images.

———————
#!/bin/bash

# Author: Hans-Henry Jakobsen
# Date: December 2007
# Homepage: http://pario.no
# E-mail: hanshj@pario.no

# Please give me a note if you find this little script usefull or make some fun changes to it.

# Description:
# Script to resize JPG images to desired width defined in IMAGESIZE variable.
# EXIF tags is also removed from the result images.
# Software needed:
# jhead – http://www.sentex.net/~mwandel/jhead/
# imagemagick – http://www.imagemagick.org

IMAGESIZE=”640″
for IMAGEFILE in $(ls|grep JPG)
do
    for I in $IMAGESIZE
    do
        # create directories if needed
        if [ ! -d $I ]
        then
            mkdir $I
        fi
   
        # Strip EXIF tag information from source file
        jhead -purejpg $IMAGEFILE

        # Resize file
        base=`basename $IMAGEFILE .JPG`_Resized_$I.JPG
        convert $IMAGEFILE -resize $I $base

        # Watermark the file
        width=`identify -format %w $base`
                convert -background ‘#0008’ -fill white -gravity center -size ${width}x15 \
            -font Verdana -pointsize 10 \
            caption:”Indian Linux Users Group, Chennai – May 2011 Meet” \
            +size $base +swap -gravity south -composite $I/$base;
   
        # Delete resized image file
            rm $base
    done
   
    # Delete source file
    rm $IMAGEFILE
done

—————————–

The variable IMAGESIZE=”640″ fixes the image size.
It resizes all the images to given value.
If you want multiple sizes, you can give as IMAGESIZE=”1024 480″

The value of “caption” will be added in the bottom border.

Here is a sample image

Backup the original images as they will be deleted at the end of the operations.
You can tweek the script to map to your requirements.

Thanks for the open source community that makes our life so easy and simple.

4 thoughts on “Resize and Add Caption to multiple images using bash script

  1. Pingback: The JPG versus RAW debate….again « Mike Hardisty Photography

  2. Pingback: How To Shrink a Picture to Avatar Size - 1 BEST 1

  3. Pingback: Online Image Resizing For Your Digital Photos | adobe photoshops

  4. Pingback: The JPG versus RAW debate….again….and again | Mike Hardisty Photography

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s