CLI tricks: Creating image thumbnails

[ 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

The value 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

The 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 
  convert -resize 200 "$file" thumb_"$file"

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.

Advanced features

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 -quality flag (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 convert function: 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 :)

More resources

About the Author

Borys Musielak creator and editor in chief. Professionally -- J2EE consultant in London City. Personally -- free software enthusiast and lobbyist.

New AdTaily ads!

Are you a film buff?

film buffs community, movie recommendations and reviews

RSS: Comments

You can follow the comments to this article through a special channel RSS 2.0 .

Related articles: CLI

 more »

Related articles: Graphics

 more »

Related articles: HOWTOs

 more »

PolishLinux Top Content

Become our fan on Facebook! on Facebook

Follow PolishLinux on Twitter!

Follow polishlinux on Twitter

Google Ads