ImageMagick & PerlMagick

ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in over 100 formats. ImageMagick can translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems and offers an iterface to most programming languages (Java, php, Perl, Pascal, Ruby, Python, C++, …).

The Definitive Guide to ImageMagick explains all of these capabilities and more in a practical, learn-by-example fashion, the book ImageMagick Tricks by Sohail Salehi is a fast-paced and practical tutorial packed with examples of photo manipulations, logo creation, animations, and complete web projects.

PerlMagick is an objected-oriented Perl interface to ImageMagick. This module can read, manipulate and write an image or image sequence from within a Perl script. This makes it very suitable for Web CGI scripts. I created the following test script on my hosted webserver to verify the correct configuration of ImageMagick and PerlMagick.

#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Image::Magick;
# created by Marco Barnig on 6th january 2009

my $headline = “Perl Test Script”;

print “Content-type: text/htmlnn”;
print ‘<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>’, “n”;
print “<html><head><title>Perl Test</title></head><body>n”;
print “<h1>$headline</h1>n”;
print “<p>This test script opens an image file test.jpg, makes a clone, modify it and save it as mynew.gif</p>n”;

my $image1 = new Image::Magick;
my $status = $image1->Read( “../../httpdocs/media/test.jpg” );
die “Couldn’t open file test.jpg !” if “$status”;
my $image2 = $image1->Clone();
#blah, do something to $image2
$image2 -> Draw (
stroke    => “red”,
primitive => “line”,
points    => “20,20 180,180”);
my $status3 = $image2->Write( “gif:../../httpdocs/media/mynew.gif” );
die “Couldn’t save file mynew.gif in folder httpdocs/media !” if “$status3”;
print “<img src=””></img>n”;
print “<br/><br/>”;
print “<img src=””></img>n”;
print “<br/><br/>”;
print “Das Script ist fertig”;
print “</body></html>n”;