Working with images from the command line

Posted by Steve on Sat 25 Dec 2004 at 23:32

There are a lot of times when it's convenient to work with images without having to wait for big graphics editors to startup. Simple operations which are common for image galleries such as rotating, resizing and adding comments to images can all be performed from the command line.

Probably the most versatile collection of tools is found in the imagemagick package.

Install it by running the following command as root:

apt-get install imagemagick

Once you you have it installed you have a great tool for working with images.

As part of the suite there is a general purpose tool called mogrify which allows you to modify, or mogrify, an image. You can also use the convert command to convert one image to another, applying changes such as resizing or adding text.

For starters we'll need an image to work with, almost all formats are acceptible, .png, .jpg, .gif, .tiff, etc.

When you have an image you can display it upon your screen by running:

display filename.gif

This will popup a window showing you the image, click upon it with your left mouse button and a menu will popup, the transform sub-menu will allow you to perform simple operations such as rotating it, or cropping it.

Those operations can also be carried out from the command line. For example if you have an image file steve.jpg you can rotate it 90 decrees to the right by running:

mogrify -rotate 90 steve.jpg

If you wish to save it as something else you could run:

convert -rotate 90 steve.jpg newfilename.png

Notice that the formats of the input and output file don't have to match?

Another common operation is resizing an image, to create a thumbnail for displaying upon a webpage for example. This can be accomplished by running:

convert -resize 100x100 steve.jpg thumbnail.jpg

This will create a new image called "thumbnail.jpg" which is 100x100 pixels, scaled from the original input file.

If you wish to add text to a file you could do this too:

convert -font helvetica -fill black -draw "text 10, 10 '`date`'" steve.jpg out.jpg output.jpg

This adds the current date to the image, using the helvetica font, in a black background at the coordinates 10,10 - with 0,0 being the upper left of the image.

Running these commands individually is a little wasteful, but you can apply the operations to each file in a directory with a simple shell script.

The following script will create a subdirectory called thumbs and put thumbnails of each .jpg file in the currect directory:

#!/bin/sh

#
# Make a thumbs sub-directory if one doesn't already exist.
#
if [ ! -d thumbs ]; then
  mkdir thumbs/
fi

#
# For each .jpg file in the current directory
#
for i in *.jpg; do

    #
    # Create a thumbnail at 200x200 pixels in the thumbs/ directory
    #
    convert -resize 200x200 $i thumbs/$i

# Done
done

This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):

This article is copyright 2004 Steve - please ask for permission to republish or translate.