ImageMagick Tips #

As I continue working on the site that I developed the magnifier for, I've reached a point where the basic design is pretty much in place, and the time to automate the process has come. The 160 or so bindings are divided up into 26 cases. In addition to each case page, each binding has its own magnifier page (I realize that I could have only one magnifier page that takes the images to use as arguments, but this would have meant requiring either JavaScript or a CGI script, neither of which was an option). To generate the HTML as well as all the images, I turned to Perl and the Image::Magick module that packages ImageMagick's functionality in a programatically-accessible manner.

Getting ImageMagick performing in quite the way I wanted required a few tweaks. I was using a pre-packaged build of ImageMagick for Mac OS X. However, this is of version 5.5.7, while the latest version (of the main program as well as of the Perl module) is 6.0. Upgrading my ImageMagick install wasn't something that I relished doing, and thus the only other alternative was to get the older version of the module (since I'm not sure if it's possible to do this through the CPAN shell, I downloaded and installed it by hand). The module requires a few libraries to be present, specifically libjpeg, libpng, libtiff, and liblcms. This guide details how to build and install the first three. Note that it points your directly to downloadable archives; browsing through the linked sites may yield more recent versions. liblcms (Little Color Management System) is also downloadable and builds fine without any modifications. Installing all of these libraries in a consistent location such as /usr/local is helpful when tracking them down later. After they are all in place, Image::Magick should build and install fine.

There is some documentation for Image::Magick, and although it could be a bit more thorough (e.g. what do some of the more obscure scaling filters do), it's generally sufficient. One issue encountered when saving JPEG's was that their quality was abysmal when compared to Photoshop's (i.e. at the same file size, compression artifacts were much more visible). The library itself doesn't do the compression, rather (as expected) it relies on libjpeg for this. As pointed out by Alexey, other programs that also use libjpeg, such as GIMP save JPEG's that are comparable in quality to Photoshop's. The difference appeared to be that ImageMagick didn't make use of libjpeg's optimized mode (as specified by the optimize_coding flag). The final solution was to have Image::Magick save the file in a lossless format such as PPM, and then use the cjpeg CLI utility (provided as part of libjpeg) to do the JPEG compression. It supports varying levels of quality, optimization, and precision, and thus, with the right amount of tweaking, provides much better results. The actual code looks something like this:

sub SaveJPEG
    my ($image, $path, $quality) = @_;
    `cjpeg -optimize -dct float -quality $quality /tmp/out.ppm > "$path"`;

Post a Comment