[ Thursday, 26 October 2006, michuk ]
If you publish content on the web you need to create thumbnails of published images every now and then. GIMP is useful for that, but why use such a heavy tool for such a simple task? It always took me about a minute to actually launch GIMP, do all the clicking to get a small version of the image and save it to another file. I have been doing it this way for ages, until I have heard of ImageMagick for the first time.
Creating a single thumbnail
Imagemagick is a simple but very powerful set of command line tools for image manipulation. It can do way more than only creating thumbnails, but for the sake of this article, I will focus on this feature only.
The Imagemagick’s basic command for manipulating images is
convert. Creating a thumbnail is as simple as typing the following:
convert -resize 200 image.png "thumb_image.png
200 is the width of the image thumbnail in pixels. You can use percentage values here as well. Easy enough? And takes only a fraction of the time I used when doing the same thing in GIMP. But… this is just one image. What if we want to create thumbs of all images in our folder?
Massive thumbnail creation
convert program does not offer a way to manipulate many images at the same time. But, this is no problem since we are in the Linux console and we have access to the powerful shell scripting language: bash. Creating thumbs of all images in some folder is as easy as calling the same
convert function in a simple loop.
for file in *.png do convert -resize 200 "$file" thumb_"$file" done
The above script will create thumbs of width 200px for all the PNG images located in the current folder. What if you have JPEG images instead of PNGs? Well, you can just replace the extension in the loop with the one you like, since ImageMagick works with all common image formats.
Another option for such task is to use the
mogrify script that comes with ImageMagick as well. A sample usage could look like this:
mogrify -resize 200 *.png
The important difference is that
mogrify overwrites the original image file, whereas,
convert writes to a different image file. Thanks for memals for pointing this out.
You already know the basics. Here are some more tricks you might find helpful when dealing with the images in the command line…
- You can control the quality of thumbnails by using the
-qualityflag (for JPEGs the value can be set between 0 and 100).
- In case of JPEG files you can also try the djpeg/pnscale/cjpeg combo. This can give you slightly better quality in case of very small thumbnails (keeping the file size small as well). A typical command would look a bit more complicated to the
djpeg -ppm < "$file" | pnmscale -xysize 200 400 | cjpeg -optimize > "thumb_$file"
If you know any more useful tricks, please use the comments area below to show off
- ImageMagick official home page
- Convert manual from the ImageMagick’s website
- ImageMagick: A graphics wizard for the command line — Adding text, frames and other effects to the images, extracting images from PDF documents and more
- ImageMagick, Part One — an article from Linux Magazine
Subscribe to RSS feed for this article!