Batch edit images with Imagemagick

It’s easy to edit multiple images from the command line with ImageMagick. You can rotate, resize, or crop without having to open up any slow programs. We’ll use a bash one-liner — a simple one-line shell script — to accomplish this.

You’ll want to start with a folder with nothing in it but the images that you want to edit.  Make a folder inside your images folder, called “edited”.

A folder with some images in it

A folder with some images in it

First, you’ll want to create an echo statement. This lets you preview what your one-liner is going to do.

for i in `ls *.JPG`; do echo "convert -resize 500 $i edited/$i"; done

This should give you some output like this:
convert -resize 500 DSC_0257.JPG edited/DSC_0257.JPG
convert -resize 500 DSC_0258.JPG edited/DSC_0258.JPG
convert -resize 500 DSC_0259.JPG edited/DSC_0259.JPG

Let’s break down this output.  We’re seeing three “convert” commands – one for each image in the folder. Convert is the main ImageMagick program. The first argument is “-resize 500″, which tells convert to resize a file to 500 pixels. That’s followed by the original filename, and then the output file.

The first “echo” step is important – especially if you’re new to using the command line – so you can check what your commands will do before you run them.  If the commands look right, now you can remove the “echo” part (along with the quotes, which tell the echo command exactly what to print):
for i in `ls *.JPG`; do convert -resize 500 $i edited/$i; done

Your “edited” folder should now have copies of your images, resized to 500 pixels.

If imagemagick isn’t installed, you can install it on an Ubuntu system by typing
sudo apt-get install imagemagick

  1. No comments yet.

  1. No trackbacks yet.