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

Subscribe to RSS feed for this article!


fold this thread memals  Monday, 30 October 2006 o godz. 1:42 pm #  Add karma Subtract karma  +0

mogrify can do most of the simple convert commands and will operate on more than one file
mogrify -resize 200 *.jpg
if you have convert you will also have mogrify

(Comments wont nest below this level)
fold this thread michuk  Monday, 30 October 2006 o godz. 1:54 pm #  Add karma Subtract karma  +0

Thanks memals, I updated the article to include mogrify as well. I never use it though, since it overwrites the original file(s).

(Comments wont nest below this level)
fold this thread cultrocker  Monday, 30 October 2006 o godz. 4:52 pm #  Add karma Subtract karma  +0

Don’t forget the -thumbnail option of convert. It is used to do exactly this. It changes the size of the image to the value specified and strips the unwanted meta infomation off of the jpeg files.

convert infile.jpg -thumbnail 240×180! outfile.jpg

When processing many large files use the -size hint at the begining of options list. This speeds up conversion because convert only reads the file until enough of the image data is available to produce the lower resolution image. Try this.

convert -size 240×180 infile.jpg -thumbnail 240×180! outfile.jpg

This can will run much faster since the complete input file is not read. From the docs;

“use this option to choose a particular resolution layer of a JBIG or JPEG image (e.g. -size 1024×768)”

(Comments wont nest below this level)
fold this thread Fredrik Rambris  Friday, 3 November 2006 o godz. 4:40 pm #  Add karma Subtract karma  +0

A thing I did before creating my own thumbnailer was to use jpeglibs own scaler, which is very much faster than loading the whole image, scaling it down and then compressing it again.

djpeg -scale 1/8 $file | cjpeg -outfile thumb_$file

Or if you want it to be an exact size but not one that is perfectly dividable with the original size you first scale it to approximatly that size with djpeg and then use pnmscale to do the final bit. It is faster than just using pnmscale or convert for that matter.

djpeg -scale 1/8 $file | pnmscale -xysize 64 64 | cjpeg -outfile thumb_$file

(Comments wont nest below this level)
fold this thread warpman  Tuesday, 7 November 2006 o godz. 1:01 am #  Add karma Subtract karma  +0

If you think it’s time for practical use call on to my Warpman’s WWW. I wrote three scripts: two to make screenshots and one to convert the pictures. The text describing the scripts is supplemented with a dozen or so screenshots. Have a nice reading and of course give ‘em a try!.

(Comments wont nest below this level)
fold this thread Josu Lazkano  Wednesday, 21 February 2007 o godz. 10:58 am #  Add karma Subtract karma  +0

how can I resize a lot of images inside a folder and that folder is near a lot of folder in other folder.

I don’t know if you understand me, something like this:



(Comments wont nest below this level)
fold this thread michuk  Friday, 11 May 2007 o godz. 10:53 am #  Add karma Subtract karma  +0

New article about ImageMagick with a lot more info: Enchanting Pictures with ImageMagick

(Comments wont nest below this level)
fold this thread Amateur African Babes  Saturday, 1 December 2012 o godz. 6:14 am #  Add karma Subtract karma  +0

But Pecola’s mother completely ignored her feelings of pain and instead continued the comforting of her white “daughter”. Pauline on the other hand, chose an identity she could be content with; so she was somehow satisfied with her identity. They spend a lot of time and money on it so she will really appreciate it when you tell her how much you like it.

(Comments wont nest below this level)
fold this thread Rufina Stackhouse  Monday, 4 February 2013 o godz. 11:19 am #  Add karma Subtract karma  +0

Hi there, yeah this article is really pleasant and
I have learned lot of things from it regarding blogging. thanks.

(Comments wont nest below this level)
fold this thread google  Saturday, 20 April 2013 o godz. 9:08 pm #  Add karma Subtract karma  +0

In my mind this was not rocket science and one thing was certain
‘ the kitten clearly needed shelter. It was a very uncomfortable 7 days where I kept my eye closed most of the time. The benefits of colloidal silver water has not yet been scientifically proven.

(Comments wont nest below this level)
fold this thread site  Tuesday, 30 July 2013 o godz. 6:37 pm #  Add karma Subtract karma  +0

I have read so many articles regarding the blogger lovers
but this article is really a fastidious post, keep it up.

(Comments wont nest below this level)
Name (required)
E-mail (required - never shown publicly)

Adjust field size: shrink | enlarge)

You can use simple HTML in your comments. Some examples are as follows:
  • A hyperlink: <a href="">GNU/Linux for everyone!</a>,
  • Strong text: <strong>Strong text</strong>,
  • Italic text: <em>italic text</em>,
  • Strike: <strike>strike</strike>,
  • Code: <code>printf("hello world");</code>,
  • Block quote: <blockquote>Block quote</blockquote>

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