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

del_icio_us Save to del.icio.us
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


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

WSH2EXE part 2

Preventing Windows Screen-Saver Activation

Open Source Licenses

Preserving my Favorite HN Links

WSH2EXE part 1

An Interview with Brad Templeton

Windows Text to Speech in WSH JavaScript

An Embedded Mini-Interpreter

Compiling Rhino JavaScript to Java

E-mail cleansing


Full List of Posts

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


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer