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 an image sequence on any X server.
mathematically and visually annotate the difference between an image and its reconstruction.
overlap one image over another.
interpret and execute scripts written in the Magick Scripting Language (MSL).
convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.
display an image or image sequence on any X server.
describe the format and characteristics of one or more image files.
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.
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.
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.
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:

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


# Author: Hans-Henry Jakobsen
# Date: December 2007
# Homepage:
# E-mail:

# 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 –
# imagemagick –

for IMAGEFILE in $(ls|grep JPG)
    for I in $IMAGESIZE
        # create directories if needed
        if [ ! -d $I ]
            mkdir $I
        # 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
    # Delete source file


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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s