How to bulk convert images using ImageMagick

ConfigCategory
4 min read
Cameron Laird

It’s easy to change the file format of images on a laptop or notebook. But what about when you’ve got thousands of images on a hosted server? I recommend ImageMagick because it’s:

  • Free — ImageMagick is a free software package that runs well on any hosted version of Linux.
  • Safe — The application carefully scans input images for harmful infections, and has a good record for resisting infection.
  • Easy — Simple shell scripting makes it easy to apply ImageMagick commands to portfolios of hundreds or thousands or even millions of images at a time.

Tedious reformatting should never be an obstacle to reworking graphical presentation: bash easily combines with ImageMagick to produce results in minutes (versus hours, days, decades if done manually). Plus, you don’t have to be a wizard to pull off mass conversion. The recipes below are adequate models to solve nearly all elementary image transformation requirements you might run into with your site.

How to install ImageMagick

To set this up, install ImageMagick by logging in to the server, and requesting the following:

sudo yum install ImageMagick

Capitalization must be as shown above. Plus, you’ll need about 40 to 60 MB of free space.

Common image conversion situations

The need to reduce file size and organize images are the two most common reasons for converting images. Read on for simple instructions.

Converting large images for faster viewing

Let’s say you have a folder (called $FOLDER, for sake of example) with 800 large tiff photographs which might be 30 MB lossless files. Because this will take some time to download and display in a browser, you can create smaller jpegs for faster viewing with the following operation:

cd $FOLDER
convert *.tiff *.jpg

Afterward, $FOLDER will contain the 800 original tiffs, along with 800 new jpegs with the .jpg suffix.

Converting images for improved organization

ImageMagick supports a number of options to help organize images. One example involves converting .tiff files to .jpg. Here’s how:

cd $FOLDER
convert *.tiff ready-for-web/converted.jpg

This renames the results to:

ready-for-web/converted-0.jpg, ready-for-web/converted-1.jpg

Another example involves writing results to a new $IMAGES folder, but retaining the original filenames with:

cd $FOLDER
   for PHOTO in *.tiff
   do
       BASE=`basename $PHOTO`
	convert "$PHOTO" "$IMAGES/$BASE.jpg"
   done

This script transforms filenames such as landscape.tiff and NYC-meeting.tiff to $IMAGES/landscape.jpg and $IMAGES/NYC-meeting.jpg, and so on.

Scripting

The commands above are all expressed in a conventional way for command-line interaction. An example such as:

sudo yum install ImageMagick

for instance, abbreviates, “use ssh to log into the server, then enter sudo yum install ImageMagick at the ‘shell’ or ‘bash’ command prompt.” An alternative reading, appropriate for longer sequences such as the six-line for loop above, might be, “use ssh to log into the server, then create a text file my_script.sh whose contents are the script provided, then use bash my_script.sh to execute that script.”

The point here is that these operations are entirely conventional: bash makes it mundane to script such tiny little command sequences and save hours of tedium that otherwise would have to be done manually.

Sizing

One common use of ImageMagick is to rescale images, such as to create thumbnails. This can be combined with other effects like format conversion. For instance:

convert '*.tiff[180x>]' thumbnail180-%02d.jpg

produces several distinct effects in one pass. This command produces images:

  • Named thumbnail180-00.jpg, thumbnail180-01.jpg, and so on.
  • Rescaled to 180 pixels wide.
  • That retain the original aspect ratio of height to width.
  • That are not rescaled at all if the original was less than 180 pixels in width.

ImageMagick on a hosted server

ImageMagick supports many other operations. convert alone allows for dozens of other effects, including masking, grayscale, compression quality, and much more. Check out The Definitive Guide to ImageMagick for full details on its capabilities. Given the price of ImageMagick (free!), it’s a must for your toolbox.