ImageMagick is a free, open source, image application. Information is available at:
http://www.imagemagick.org/Usage/
If you use Windows, a compiled binary download is available:
Perl, Python, and Ruby each have their own interfaces to ImageMagick.
Today's blog provides equivalent implementations of the ImageMagick montage feature, in three languages.
Perl:
#!/usr/bin/perl
use Image::Magick;
my $image = Image::Magick->new;
my $montage = Image::Magick->new;
my $status = $image -> Read("mach_cov.jpg",
"071.jpg", "072.jpg", "073.jpg", "075.jpg",
"076.jpg", "077.jpg", "079.jpg");
$image -> Scale('200x200');
print STDERR $status;
$montage = $image -> Montage(background => "Blue",
borderwidth => "5", geometry => '+5+5',
tile => "4x2");
$montage -> Write('jpg:orig.jpg');
system("imdisplay orig.jpg");
exit;
Python:
#!/usr/bin/python
import os
os.system("montage mach_cov.jpg \
071.jpg 072.jpg 073.jpg 075.jpg \
076.jpg 077.jpg 079.jpg \
-background #0000ff -geometry 200x200+5+5 \
-borderwidth 5 -tile 4x2 orig.gif")
os.system("imdisplay orig.gif");
exit
The "\" appearing at the end of lines is the DOS command-line continuation character.
Ruby:
#!/usr/local/bin/ruby
require 'RMagick'
include Magick
orig = Magick::ImageList.new(
"mach_cov.jpg", "071.jpg", "072.jpg",
"073.jpg", "075.jpg", "076.jpg",
"077.jpg", "079.jpg")
new_img = orig.montage() {
self.background_color='blue';
self.border_color='red';
self.tile='4x2'
self.geometry='200x200+5+15'
self.border_width=5}
new_img.write("orig.gif")
system("imdisplay orig.gif")
exit
The input images are the 8 book covers that appear on the right hand side of this blog. Obviously, you'll substitute any number of images from your own collection. You'll also need to provide a an appropriate tiling geometry for the number of images you use (I used 4x2, four across and 2 down) for this example.
The output looks like this:

Each of the scripts calls provides the list of images to be included in the output image, and provides the montage method with attributes for the image display (i.e., background collor, image sizes, image border, space between images, tiling geometry).
In the case of the Perl script, the external module Image::Magick is called (available from ActiveState). In the case of Ruby, the RMagick gem is used. Users of these languages should known how to acquire these modules.
In the case of Python, a system call is made to the installed ImageMagick application. I had a little problem getting the Python script to work because the system call to the "montage" executable apparently executed some other program of the same name. This problem was skirted when I simply visited the subdirectory that held the ImageMagick files, and copied the montage.exe file over to the same subdirectory that held my Python script. When the python script was interpreted, the system used the montage.exe file in the same subdirectory as the script.
The montage method is explained in detail on the following web page:
http://www.imagemagick.org/Usage/montage/
Many of the basic algorithms of Medical Informatics, with equivalent implementations in Perl, Python, and Ruby are provided in my book, Methods in Medical Informatics: Fundamentals of Healthcare Programming in Perl, Python, and Ruby, which will be published next month by CRC Press.
- © 2010 Jules J. Berman