Friday, July 07, 2006
Bulk resizing and renaming images in Ubuntu - The Resize_images Nautilus script
I take a lot of pictures, and thus image manipulation is one of the more frequent tasks I do on a computer (though I'm by no means a pro). Since I'm in the process of switching to using Ubuntu, in the past few days I've been learning how Ubuntu handles images.
Importing images from my camera has been easy (as soon as my camera is plugged into a USB port, a dialog box pops up asking me what I want to do); previewing, rotating, and moving images is extremely easy to do via gThumb; and advanced image editing can be done via GIMP (which is similar in complexity to Photoshop). However, one thing that hasn't been easy is figuring out how to resize lots of images at once.
When resizing images for the web I usually create a thumbnail (~200 pixels high or wide) and a larger image (~600 pixels high or wide). To make file management easier I typically append the image's size to the file name. So, for instance, picture.jpg would become picture_200.jpg (a 200-pixel wide or high thumbnail) and picture_600.jpg (a 600-pixel wide or high image). This can be relatively tedious to do in most programs, especially with large numbers of images.
GIMP can handle scripts and plugins, but a quick look through their repositories didn't find anything that looked right (note: I probably missed something), and gThumb doesn't have a bulk resizing feature at all. ImageMagick is a (very powerful) command-line based program that can handle many images at once, and many people have written command-line-based scripts that make ImageMagick easier to use (the most versatile of which is bbips). However, all the scripts I initially found needed to be run from the command line, and none of them would rename the files as I desired.
Then I found G-script, a website that distributes scripts designed to integrate directly with GNOME (Ubuntu's graphical interface, equivalent to Windows) or Nautilus (Gnome's file manager, equivalent to Windows Explorer). G-script's multimedia category featured a script (NIS) that would resize images from within Nautilus. I installed it1, but quickly found that it wasn't quite what I was looking for; while it did have a graphical interface, it didn't have enough resizing options (only had maximum dimension lengths of 160, 320, 640, 800, and 1024 pixels), and while it did move the resized files into a new directory, it didn't rename the files.
In Windows I probably would have been out of luck, but since this is Linux and the script was contained in a text file (and licensed under the GNU GPL), I could freely modify it. So, last night I set about learning the basics of modifying a shell script, and eventually created a script that did everything I wanted.
The new script ("Resize_images"; download it here3) is run as a Nautilus script; once it's installed1 all you have to do is select an image (or multiple images) in the file browser, select the script (by right clicking on the images and looking under the scripts menu), and the script does the following things:
- Presents the user with a list of maximum dimensions2 (e.g., 1024, 800, 600, 400 pixels) that images can be resized to. Images will be resized so that their longest dimension is set at this value; the image's other dimension will be scaled appropriately (with a pre-set quality of 80).
- Resize (theoretically) as many jpgs and gifs as can be selected in Nautilus at once.
- Puts all resized images into a subdirectory named "resized_to_xxx" (where xxx is the maximum dimension that was selected).
- Appends the selected maximum dimension to the filename of resized images. So, if images are resized to a maximum dimension of 600 pixels, "caterpillar.jpg" becomes "caterpillar_600.jpg" and "cute_rat_yawn.JPG" becomes "cute_rat_yawn_600.JPG".
- The script does not modify the original images in any way.

The primary dialog of the Resize_images script.
[Downloaded the script here3.]
The script is run from Nautilus (Ubuntu's basic file browser), so to resize images all I have to do is find them with the file browser, select them, and execute the script (which only requires right-clicking on the image(s) and selecting the script from the "Scripts" menu that appears). That's it. I can now resize dozens of images with just three clicks of the mouse (assuming the images are already selected); I don't have to open Photoshop, GIMP, or any other program.

Choosing the Resize_images script from within Nautilus (Ubuntu's file browser) by right-clicking on the image.
This is a great example of the power of open-source (and easily scriptable) operating systems. I had a specific task I wanted to carry out, but even though I couldn't find a pre-written program to perform that task, I found something that did most of what I wanted and then just modified it to make it "perfect." And, since everything is open-source, now I can share my completed script so that others who want to do the same thing can do it more easily, and also so that others can improve upon what I've done (something that's desperately needed, I'm sure).
Notes:
1 To install the script you must first save it to a file, make it executable ("chmod u+x Resize_images"), and then copy it to your Nautilus scripts folder (~/.gnome2/nautilus-scripts). At that point it should be visible in Nautilus when you right click on an item and select "Scripts". The script does require ImageMagick, zenity, and rename to run, but all of these are installable on Ubuntu (and zenity and rename should be installed by default).
2 The list of maximum dimensions can be customized by editing the script file - as many (or as few) options as the user wants can be included, and they can be listed in whatever order the user desires.
3 Keep in mind that I'm a complete novice at scripting, so use at your own risk. I'd suggest testing this on copies of images before trying it on originals. If anyone has improvements to the script, I'd love to see them.
[Updated July 15, 2006 to add screenshots of the script.]
[February 19, 2007 - Updated the script (to v0.75) to support Edgy; many thanks to Dave for finding the required fix. The links in the post now point to v0.75, and a tar.gz of v0.75 can be found here. Version 0.7 can be found here.]
Labels: Linux, photo, script, ubuntu













