Creating Photo Galleries With ImageMagick
by Liam McDermott, 30 November 2006 - 5:53am
Part of creating a web site often involves creating photo galleries of the clients products. Usually this means the designer recieves a number of photos and must resize them into thumbnails, then link them to larger pictures.
This is a boring and laborious process; photo gallery software is available (e.g. Gallery etc.), but is excessive for simple projects that are not updated often.
This web designer spent an evening (and early morning) hacking and came up with: ‘Thumbnailor’ (spelling intentionally incorrect). Thumbnailor is a small script for resizing lots of images, it was written as a Linux script that requires ImageMagick and is—of course—distributed under the GNU GPL.
You don't need to know what all that means. Although a working knowledge of Linux (or similar OS) helps.
To use Thumbnailor, complete the following steps: —
- Get a copy of ImageMagick. If you’re unsure whether you have it already, try typing:
convertat a command line. If you get information on how to use convert, and details of ImageMagick’s web site, then it is installed.
- Download the file attached to the article, save it as ‘thumbnailor’, somewhere your Linux system will be able to execute it (e.g. ~/bin/thumbnailor or /bin/thumbnailor, recommended is the bin directory in your home directory, create this if it does not exist).
- Open up a terminal and navigate to the directory where Thumbnailor is stored, e.g.
cd ~/bin/. Then give permission for the script to execute by typing:
chmod +x thumbnailor. You may need to be the root user to do this, depending on where you saved the file.
- Open a terminal and type: ‘thumbnailor’. The program should print an error message and usage statement. This means we have successfully installed the script. Possible problems at this stage are: —
- A ‘Permission Denied’ error is printed to the screen. This is caused by incorrect permissions, has step two been completed successfully?
- ‘Command not found’ is printed to the screen. Your Linux system doesn't look for programs where you put the script. Not an issue; type the entire path to the script, not just the name, e.g. ~/downloads/thumbnailor. Do this every time the script needs to be run.
- Make a copy of some images you’d like to make a photo gallery from (it's not wise to use originals, and remember there's no warranty with this script!).
- Using the command-line: navigate to where the images are stored. For example, if they are stored under ~/images/thumbtest/ type:
thumbnailor "*.jpg" 150 70. Assuming the images you wished to convert were JPEG’s, you should see a new directory named ‘thumbs’ appear. The thumbnails are in there, open the directory and view some of the files using your favourite image viewer. If you recieve a: ‘command not found’ error, the full path to thumbnailor needs to be entered when running it, e.g.
~/downloads/thumbnailor "*.jpg" 150 70.
- As shown, Thumbnailor needs three arguments to work. These are: —
- A list of files, e.g. "*.jpg". The double-quotes must be included or strange things happen. The script has only been tested with JPEG files, but may work with other types too.
- The size of the thumbnail, e.g. 150. Thumbnailor currently generates square thumbnails only. Resizing a rectangular image using ImageMagick is easy, the need for Thumbnailor is in the cropping.
- Quality of the output files, e.g. 70. A value between 1 and 100 is required.
- Try Thumbnailor for yourself, test it with different settings.
This should be enough information to start using Thumbnailor. This is the first time the script has been published so probably has a number of bugs, but if used responsibly could be very useful.
It was created for a production project, where it was used to good effect, hopefully others can also put it to good use too, and if you find a problem: let me know!
During that production project I also used ImageMagick from the Linux command-line to resize the larger images in the gallery. This will be explored in another article, later in this series.