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
tags:image files, perl programming, Python programming, Ruby programming
Science is not a collection of facts. Science is what facts teach us; what we can learn about our universe, and ourselves, by deductive thinking. From observations of the night sky, made without the aid of telescopes, we can deduce that the universe is expanding, that the universe is not infinitely old, and why black holes exist. Without resorting to experimentation or mathematical analysis, we can deduce that gravity is a curvature in space-time, that the particles that compose light have no mass, that there is a theoretical limit to the number of different elements in the universe, and that the earth is billions of years old. Likewise, simple observations on animals tell us much about the migration of continents, the evolutionary relationships among classes of animals, why the nuclei of cells contain our genetic material, why certain animals are long-lived, why the gestation period of humans is 9 months, and why some diseases are rare and other diseases are common. In “Armchair Science”, the reader is confronted with 129 scientific mysteries, in cosmology, particle physics, chemistry, biology, and medicine. Beginning with simple observations, step-by-step analyses guide the reader toward solutions that are sometimes startling, and always entertaining. “Armchair Science” is written for general readers who are curious about science, and who want to sharpen their deductive skills.