Batch conversion of images on Linux

I recently had a need to convert 3,000 scanned TIFF images to a more sensible format for distribution, such as JPEG. I’ve been using GIMP to edit the individual photos but sadly it doesn’t have a batch format conversion tool.

So I wrote my own little script in bash. You will need to install ImageMagick (a commandline image editor) on your box first, but this is bundled with most distributions and be installed from a repository.

Things to be aware of when using this script:

  • It does not recurse into subdirectories – you have to place the script in the same directory that the TIFFs are in
  • At the time of writing, ImageMagick is single-threaded and can only use one core of a multicore CPU. If you want to use N cores, you have to split the photos into N directories and run N copies of the script. However be aware that, in my case at least, each TIFF was around 45MB and took between 1 and 1.5 seconds to convert. This means the disk is reading at maybe 40MB/s, so running two threads you will quickly hit the limit of your disk.
  • You can change the compression of the JPG by altering the value 85 in the script below.
  • You can also convert your TIFFs to other formats. See man convert for details.

Finally, the code you’ve all been waiting for. Feel free to use, edit, distribute this anywhere you please.

#!/bin/bash

total=`ls *.tif | wc -l`
count=0
ls *.tif | while read i
do
file=`basename "$i" .tif`
echo $file
convert "$file.tif" -quality 85 "$file.jpg"
count=`expr $count + 1`
percent=$(($count / $total))
echo $percent% completed, done $count of $total
done
echo All done!

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 )

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