This site is now 100% read-only, and retired.

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:


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

# 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



There are a variety of helpful articles out there
Posted by Anonymous (203.17.xx.xx) on Fri 31 Dec 2004 at 01:04
Especially on if you need more details.

[ Parent ]

Re: There are a variety of helpful articles out there
Posted by Anonymous (149.99.xx.xx) on Wed 24 Aug 2005 at 09:01

This article does the job. Great. Thanx.
Right to the point with the exact right examples.

Perhaps GNUnix document writers will all learn yet that complete documentation is not necessarily the same thing as good documentation.

[ Parent ]

Re: Working with images from the command line
Posted by olohoyo (81.56.xx.xx) on Tue 13 Feb 2007 at 08:18
And with the package exiftran, if images come from a camera knowing it's orientation (my Panasonic Lumix does, my cell-phone doesn't):
exiftran -aip *.jpg
will rotate images that need to be.

My 2 cents.


[ Parent ]