Scaling Images with RMagick

August 27, 2006

Very early this morning, inspired by an article on the Philippines Ruby Users Group mailing list, I started experimenting with RMagick, a Ruby programming interface to Imagemagick, an open source image manipulation toolkit. Using a slightly modified version of the image scaling script from the article, I was able to reduce all of my bloated Philippines travel photos to one quarter of their original size.

I encountered two minor obstacles in trying to run the script:

  • Installing the RMagick gem before Imagemagick. I used RubyGems to install the RMagick gem, but after my first attempt, I quickly learned that RMagick requires the Imagemagick package and its various library dependencies. Without these, the RMagick native extensions fail to compile. After successfully installing Imagemagick, my second attempt to install RMagick again failed because, as the RMagick configuration log file (config.log) reported, I was missing the freetype font library. After installing freetype, my RMagick installation completed successfully.
  • Loading the RMagick gem. To get the Ruby interpreter to load the gem, the script must include the line requires 'rubygems'; you must specify -r rubygems on the Ruby command line, or you must assign string rubygems to environment variable RUBYOPT. The RubyGems User Guide explains in detail how to use one of these three environment options.
Once I managed to overcome these obstacles, I wrote a one line Bash script to apply the image scaling Ruby script to my images:

for file in Photos/Travel/Philippines/*.JPG; do sudo -u apache ./reduce_photo.rb $file; done

Et voila! Smaller Philippines photos!