Make Image Thumbnails

Use a simple script to create thumbnails from any number of images.

[Hack #2] introduced the convert utility as a means to convert between different image formats and perform basic image processing from the command line. convert also has resizing options that make it perfect for creating image thumbnails.

The primary argument used to create an image thumbnail is -thumbnail. This argument tells convert to create a thumbnail of the input image with the specified geometry. Thumbnails are scaled-down versions of the original, which means the height-to-width ratio is automatically preserved, so you need to specify only one dimension for the conversion. For instance, to create a thumbnail that has a width of 160 pixels, type:

	$ convert -thumbnail 160 image.jpg thumbnail.jpg

To create a thumbnail with a height of 160 pixels, precede the geometry with x:

	$ convert -thumbnail x160 image.jpg thumbnail.jpg

This command comes in particularly handy if you plan to upload a lot of digital photos to a web gallery, because you can script the entire operation. To create a thumbnail with a width of 160 pixels from every JPEG image in a directory, type

	$ for i in *.jpg; do convert -thumbnail 160 $i thumb-$i; done;

Each image in the directory will have a thumbnail with thumb- at the beginning of the filename. This command also works well to create a series of thumbnails of different sizes—just run two different convert commands in the script. So, to create a mid-sized thumbnail with a width of 400 pixels and a small thumbnail with a width of 160 pixels, type:

	$ for i in *.jpg; do convert -thumbnail 400 $i mid-$i; \
	convert -thumbnail 160 $i thumb-$i; done;

You can even get fancy and combine other convert arguments with your commands. For instance, to add a 10-pixel beveled grey frame around the mid-size image, and a 2-pixel grey frame around the small thumbnail, type:

	$ for i in *.jpg; do convert -thumbnail 400 -frame 10x10+2x2 \ 
	-mattecolor "#999999" $i mid-$i; convert -thumbnail 160 -frame 2x2 \ 
	-mattecolor "#999999" $i thumb-$i; done;

Get Linux Multimedia Hacks now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.