> Create fixed size thumbnails with ImageMagick

ImageMagick

ImageMagick is powerful suite of applications to create, edit, convert and compose bitmap (and some vector) images from the terminal. It’s an essential tool for any web designer if you can get your head around it.

Photoshop has a nice Batch tool but it’s extremely slow if you have to deal with thousands images. A common task is to create thumbnails from a set of pictures, and here is where Imagemagick comes to the rescue.

First get IM and install it. You’ll find the binary release for any platform accompanied with installation instructions on the IM website.

Place all the images you wish to resize in the same directory and create a new folder called thumbs under the same directory.

The IM tool we use is mogrify. It lets us apply various filters and operations to the images in just one step. Open terminal and go to the directory where you placed the images. What we are going to do is to resize all pictures to 80×80 pixels keeping proportions (no stretch or squash) and then extend the canvas size to exactly 80×80 pixels so that all thumbnails have the same size.

mogrify -resize 80x80 -background white -gravity center -extent 80x80 -format jpg -quality 75 -path thumbs *.jpg

-resize 80x80 is self explanatory. Note that the image is not stretched to fit the given dimensions.

-background white create a white canvas to the thumbnail. If the image does not fit completely the 80×80 space, mogrify will fill the empty space with the specified color.

-gravity center centers the thumbnail in the canvas.

-extent 80x80 extends the proportionally resized image to fit exactly 80×80 pixels.

-format jpg -quality 75 sets destination image format and quality.

-path thumbs places the thumbnails in the thumbs subfolder. (Otherwise the original files would be replaced).

*.jpg processes all jpeg files found in folder.

This post is just to tickle your appetite, mogrify can do much more than resizing pictures, have a look at the help page on ImageMagick website and see what you have missed so far ;)

IM literally saved my life in the past, hope it could be of some use for you as well.

/Share the joy

/Reactions

  • Thanks Matteo. I’d spent ages looking through the ImageMagick options pages for the right command (-extent) for doing exactly this. Finally came across this blog post. Much appreciated!

    A quick note that may be of use to others: Using “mogrify” and specifying the -path option is the same as using “convert” and specifying the destination file.

    • Author: Juha
    • Posted on: 2011/07/26
    • At: 15:36

    Just to let you know, CentOS (and it seems that RedHat also) has old version of ImageMagick in their repositories, so the “extent” flag doesn’t work.
    In order to get it to work, it seems that you have to manually install new version of ImageMagick.