John Squibb
Bash Image Resize with Image Magick
06 September, 2009
Whenever I rip photos from my camera to my Desktop, I find that the downloaded size is a bit heavy for general use. I rarely need my photos in the 3648x2736 resolution and the 4MB file size that my 10MP camera stores to the memory card. The world is full of GUI utilities for Windows/Mac/Linux that will import, resize, crop, and do a gazillion other things with downloaded photos, but those can often incur heavy load times, and can hog system resources. Generally, I just need a quick batch resize on a folder full of pics so that I can toss them on my website or email them to a friend. Enter Bash, and a trusty sidekick, the Image Magick convert utility.
At this point, I'll assume you're using *nix, and therefore have bash or a similar shell program readily available on your OS. To use the convert utility, you'll need ImageMagick installed. Download it using your package manager, or by installing it from source. Check out the ImageMagick website for more details on installation for your particular OS.
The following shell script wraps a bit of logic around the convert utility provided in the ImageMagick package:
Notice that some of the lines wrap to fit within this layout.
#!/bin/bash
i=0;
basename="image_prefix_";
fullsize=30;
thumbsize=10;
quality=75;
ext='jpg';
mkdir "thumbs";
mkdir "full";
mkdir "originals";
touch "links.html";
for name in *.$ext;
do
((i=$i+1));
convert $name -resize "$fullsize%" -quality $quality
"full/"$basename$i"_full."$ext;
convert $name -resize "$thumbsize%" -quality $quality
"thumbs/"$basename$i"_thumbnail."$ext;
echo "<a href=\"full/"$basename$i"_full."$ext"\" target=\"_blank\"><img
src=\"thumbs/"$basename$i"_thumbnail."$ext"\" border=\"0\"/></a><br/>" >>
links.html;
mv "$name" "originals/$name";
done;
exit;
The above code sets some resize parameters, creates a thumbnail and new full size, moves the originals to a new folder, and creates an HTML image link for each image in the directory where it is executed. The first several lines set up the resize percentages and quality settings for the photos. The percentages used were perfect for my needs, but you may have to adjust them to suit your photos. The script also uses an image prefix and counter within the loop to change pictures from Image1, Image2, ..., to My_Vacation_to_Wherever_1, etc.
This is just one of a vast number operations you can perform with the ImageMagick utilities. To Learn more about the multitude of command line options available, check out these techniques. Once you've picked up some tricks there, try modifying the above script to rotate images that were taken sideways, or crop out unimportant items in your shot. Also, add some logic to handle different dimensions and image extensions such as .png or .bmp.
Suggested Reading
PHP Design Patterns
by Aaron Saray
Seasoned programmers looking for a good book on PHP design patterns will thoroughly enjoy PHP Design Patterns.
Weighing in under 250 pages of readable content, it presents a truckload of philosophy and pattern samples in a well-condensed format, with short, consistent chapters.
The title starts off with a quick explanation of patterns, champions their usage, and by chapter 3, the user is thrust right into the Adapter Pattern.
For the next seventeen chapters, the user is given a synopsis, a problem/solution summary, a UML diagram, and code examples for each of the patterns in the discussed in the book.
Generally, the code provides a typical approach, and then a modified approach using the chapter's pattern.
Tags: Bash,image,resize,imagemagick
Short URL: http://sqb.in/o2tB