Jim Lawless' Blog


Stacking Images with PerlMagick

Originally published on: Sun, 26 Apr 2009 14:46:51 +0000

I often receive e-mails from friends containing humorous images. One of my friends had forwarded an e-mail with a few dozen very funny images attached to it. I found the pictures to be so funny that I felt like posting them to an online forum that I frequent.

Unfortunately, this forum's image-upload feature only allowed for one image at a time to be uploaded. I was hoping to upload them as a group.

I had considered trying to capture some screenshots of a screenful each. An attempt at capturing, cutting, positioning, ...etc. proved to be tedious. I wanted a solution that I could use again. My thoughts turned immediately to the Perl interface to the ImageMagick library: PerlMagick.

Please note that ImageMagick libraries exist for many different programming languages. An ActiveX version is available so that any ActiveX-aware programming language ( including VBScript and Javascript WSH scripts ) can leverage ImageMagick. I had used PerlMagick before and often find good Perl examples via web-searches. I tend to think of PerlMagick and ImageMagick synonymously.

I found a sample program on the web that used the ImageMagick Append() method. When calling the Append() method, one may set the stack parameter attribute to true to force the newly-generated image to be comprised of a top-down set of all of the images in the image object, "stacked" on top of one another.

The following code reads for JPEG images ( i1.jpg, i2.jpg, i3.jpg, and i4.jpg ) and generates an image called stack.jpg that contains all of the above:


# License: MIT / X11
# Copyright (c) 2009 by James K. Lawless
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

use Image::Magick;

$image = Image::Magick->New;
$image->Read("i1.jpg");
$image->Read("i2.jpg");
$image->Read("i3.jpg");
$image->Read("i4.jpg");

$p=$image->Append(stack=>true);

$p->Write('stack.jpg');

undef $image;

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: WSH2EXE part 2
Next post:A Quine in C


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Converting Data to XML with AWK

Jim Butterfield : The Commodore Guru

Thwarting HTTP Referer Trackbacks

Charging by the Byte

Switching a Console Window to Full Screen or Windowed Mode

A Simple Media Control Interface Script Processor

Computers I Have Known

WSH2EXE part 2

Blog Posts by Category

Generating Primes with XSLT


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service