Recently I needed to resize a number of images with my laptop computer. I had shot a number of photos with my Canon G9, and wanted to scale them down to a web-appropriate display size. I only have one Photoshop license which is in use on another computer, so I generally use GIMP for photo editing tasks on my laptop. Batch processing images with Photoshop is trivial with the image processor script. One simply launches the script, selects the source and destination folders, chooses a file type and size, and hits the Run button.
I assumed GIMP would have a similar way to allow users to batch edit images. However, this turned out not to be the case. GIMP encourages users to develop scripts and plug-ins for batch processing type tasks. After a few internet searches, I located some scripts and plugins created for GIMP that would solve the problem. However, I decided to take a different approach. I took a Python programming course at fxphd, and decided this would be a good challenge for my fledgling scripting skills.
The task is fairly straight forward. The script needs to loop through the image files in a directory and resize each image to a specified size. Traversing files in a folder should be an easy task for a script. However I wasn’t sure if Python had an image processing library, or module, to handle low level operations on image data.
Again after some internet searching, I concluded that Python does not natively support image processing, but there is an image module called the Python Imaging Library (PIL) available for such tasks. This library may be downloaded from the Pythonware website and added to an existing Python installation. Once installed, a user may then import the library to gain access to the image processing functions. The software is free to download and use. One may purchase software support, but that’s mainly for priority technical and bug fixing support for companies with mission-critical applications.
Although PIL would have worked fine for this project, I opted for another solution. ImageMagick is a an open source image processing library that I have used in a number of projects in the past. It has cross platform support and provides many programming interfaces to its image processing capabilities.
In past projects, I used the convert program provided by ImageMagick to translate between image formats. The convert program will also resize an image, along with other image translation functions. Although ImageMagick does have a Python interface, it’s easier to just call the convert program with the appropriate command line functions.
ImageMagick has a binary installation program for Windows platforms. Once installed, the ImageMagick install directory is included in the Windows Path environment variable. This means the convert program may be called from any location in the folder hiearchy. After installing ImageMagick, I then put together the following Python script:
The script imports the os module which provides functions to handle files and directories. I then set the image width to be 1000 pixels. Then I have the script loop through the file names, print out a message, and resize each image via a system call to launch the convert program with specified parameters. The script is currently set up to overwrite the images with the new file size. Therefore care must be taken to ensure the directory being processed is a copy of the original images.
To keep things simple, I didn’t even bother with command line arguments. I just hard coded the image width. The convert program will calculate the length based on the fixed aspect ratio of the image. If for some reason I decide I need a different image size, I just open the script in a text editor and set the new width size.
I could have spent more time tweaking this script with features such as command line parameters and allowing the user to choose a destination folder and a new filename pattern. However my goal with the script was to keep things simple. In the future I might beef up this script to be more user friendly, but for now I’m pleased to have just whipped something out in a hurry that solves an immediate need.