Batch Image Editing with ImageMagick

If you take a lot of digital pictures, you may find that there are times when you want to do the same thing to a group of images. You could use the GIMP to edit all of your pictures one at a time, but that is a time-consuming process, especially if you just want to make thumbnails of your vacation pictures. The good thing is that there is a simple, yet powerful, command line utility that you can use to edit photos en masse: ImageMagick.

Let's start by making sure that we have ImageMagick installed:

# apt-get update
# apt-get install imagemagick

I'm going to start by going over two of the features of ImageMagick that I use the most frequently: resizing and cropping images. There are a lot more features available in ImageMagick, and I will probably cover more of them future posts.
  • Batch Resizing Images
    Digital SLR cameras are great for taking high quality pictures, the problem is that they typically take very large pictures that are too large for a lot of uses, such as posting on a blog or using in a presentation. Generally, the first thing I will do is open one of my pictures in the gthumb or some other image viewer that allows me to zoom in and out to find the percentage I want to shrink the images to.

    The next thing I do is I will make copies of all my images that I am going to edit so that I can retain the originals; I do this in a separate directory to minimize my own confusion:

    $ cd /[path-to-images]/
    $ mkdir thumb
    $ cp *.jpg thumb/
    $ cd thumb

    Now, we just use the mogrify command with the -resize option to resize our images. Let's say we want to resize them all to 20% of their original size:

    $ mogrify -resize 20% *.jpg

    Depending on the number of images you are resizing, this command may take a little bit to run, but it is a lot faster than editing each image by hand. If you wanted to resize your images to a specific size, say to create background images for your Mini 9, we would specify 1024x600 pixels rather than a percent:

    $ mogrify -resize 1024x600 *.jpg

    That pretty much does it for resizing pictures with ImageMagick.

  • Batch Cropping Images
    For my post on Installing Debian, I ran the installation in VirtualBox and took screen shots of the VirtualBox window to get the images I posted. The problem with this was, there was some of the window showing in the screen shots that I did not want to include in my post as you can see below:

    The good news is that this window border was identical on all of my screen shots, so I just had to figure out what I needed to crop on one image, and I could use that information in ImageMagick to crop all of the screen shots that I took.

    In order to figure out what I needed to crop, I opened one of the images in the GIMP. Using either the rectangle selection or the crop tool, I selected the region that I wanted to crop out of the image. Then looking on the tool window, I was able to see the position of the top-left corner of the selection as well as the dimensions of the selection:

    The Position section, indicated above in red, is what we will use for our offset with mogrify, and the Size section, indicated above in blue, is what we will use to define the size of the area we want to crop. We just need to use the information above with the mogrify command with the -crop option. But first, we'll copy the images to keep a record of the originals:

    $ cd /[path-to-images]/
    $ mkdir cropped-images
    $ cp *.png cropped-images/
    $ cd cropped-images
    $ mogrify -crop 639x480+1+44! *.png

    The exclamation point in the above command is there to make ImageMagick move the viewport for the image to the same as the crop offset. That may sound a little confusing, so I've included a screen shot of what a picture looks like in the GIMP if you don't use the exclamation point in the above command:

    So, you are best to use the exclamation point in the command, and when you use it, you're picture will look like this:

    That's about all there is for batch cropping images with ImageMagick

See you next time.

No comments:

Post a Comment