<metaname="description"content="Use ImageMagick® to create, edit, compose, and convert bitmap images. Resize an image, crop it, change its shades and colors, add captions, and more."/>
<h1class="text-center">PerlMagick Image API for Perl</h1>
<pclass="text-center"><ahref="perl-magick.html#installation">Installation</a> • <ahref="perl-magick.html#overview">Overview</a> • <ahref="perl-magick.html#example">Example Script</a> • <ahref="perl-magick.html#read">Read or Write an Image</a> • <ahref="perl-magick.html#manipulate">Manipulate an Image</a> • <ahref="perl-magick.html#set-attribute">Set an Image Attribute</a> • <ahref="perl-magick.html#get-attribute">Get an Image Attribute</a> • <ahref="perl-magick.html#compare">Compare an Image to its Reconstruction</a> • <ahref="perl-magick.html#montage">Create an Image Montage</a> • <ahref="perl-magick.html#blobs">Working with Blobs</a> • <ahref="perl-magick.html#direct-access">Direct-access to Image Pixels</a> • <ahref="perl-magick.html#miscellaneous">Miscellaneous Methods</a> • <ahref="perl-magick.html#exceptions">Handling Exceptions</a>• <ahref="perl-magick.html#constants">Constant</a></p>
<aclass="anchor"id="introduction"></a>
<pclass="lead magick-description"><ahref="mirror.html">PerlMagick</a> is an objected-oriented <ahref="http://www.perl.com/perl/">Perl</a> interface to ImageMagick. Use the module to read, manipulate, or write an image or image sequence from within a Perl script. This makes it very suitable for Web CGI scripts. You must have ImageMagick 6.5.5 or above and Perl version 5.005_02 or greater installed on your system for PerlMagick to build properly.</p>
<p>There are a number of useful scripts available to show you the value of PerlMagick. You can do Web based image manipulation and conversion with <ahref="https://download.imagemagick.org/ImageMagick/download/perl">MagickStudio</a>, or use <ahref="https://github.com/ImageMagick/ImageMagick6/tree/main/PerlMagick/demo/tree.pl">L-systems</a> to create images of plants using mathematical constructs, and finally navigate through collections of thumbnail images and select the image to view with the <ahref="http://webmagick.sourceforge.net/">WebMagick Image Navigator</a>.</p>
<p>You can try PerlMagick from your Web browser at the <ahref="../MagickStudio/scripts/MagickStudio.cgi">ImageMagick Studio</a>. Or, you can see <ahref="examples.html">examples</a> of select PerlMagick functions.</p>
<p>If not, you must install PerlMagick from the ImageMagick source distribution. Download the latest <ahref="https://download.imagemagick.org/ImageMagick/download/ImageMagick.tar.gz">source</a> release.</p>
<p>If ImageMagick / PerlMagick configured and compiled without complaint, you are ready to install it on your system. Administrator privileges are required to install. To install, type</p>
<preclass="highlight"><code>sudo make install
</code></pre>
<p>You may need to configure the dynamic linker run-time bindings:</p>
<p>Congratulations, you have a working ImageMagick distribution and you are ready to use PerlMagick to <ahref="https://legacy.imagemagick.org/Usage/">convert, compose, or edit</a> your images.</p>
<p><b>Windows XP / Windows 2000</b></p>
<p>ImageMagick must already be installed on your system. Also, the ImageMagick source distribution for <ahref="mirror.html">Windows 2000</a> is required. You must also have the <code>nmake</code> from the Visual C++ or J++ development environment. Copy <code>\bin\IMagick.dll</code> and <code>\bin\X11.dll</code> to a directory in your dynamic load path such as <code>c:\perl\site\5.00502</code>.</p>
<p>Next, type</p>
<preclass="highlight"><code>cd PerlMagick
perl Makefile.nt
nmake
nmake install
</code></pre>
<p><b>Running the Regression Tests</b></p>
<p>To verify a correct installation, type</p>
<preclass="highlight"><code>make test
</code></pre>
<p>Use <code>nmake test</code> under Windows. There are a few demonstration scripts available to exercise many of the functions PerlMagick can perform. Type</p>
<preclass="highlight"><code>cd demo
make
</code></pre>
<p>You are now ready to utilize the PerlMagick methods from within your Perl scripts.</p>
<p>Any script that wants to use PerlMagick methods must first define the methods within its namespace and instantiate an image object. Do this with:</p>
<preclass="highlight"><code>use Image::Magick;
$image = Image::Magick->new;
</code></pre>
<p>PerlMagick is <var>quantum</var> aware. You can request a specific quantum depth when you instantiate an image object:</p>
<p>Next you will want to read an image or image sequence, manipulate it, and then display or write it. The input and output methods for PerlMagick are defined in <ahref="perl-magick.html#read">Read or Write an Image</a>. See <ahref="perl-magick.html#set-attribute">Set an Image Attribute</a> for methods that affect the way an image is read or written. Refer to <ahref="perl-magick.html#manipulate">Manipulate an Image</a> for a list of methods to transform an image. <ahref="perl-magick.html#get-attribute">Get an Image Attribute</a> describes how to retrieve an attribute for an image. Refer to <ahref="perl-magick.html#montage">Create an Image Montage</a> for details about tiling your images as thumbnails on a background. Finally, some methods do not neatly fit into any of the categories just mentioned. Review <ahref="perl-magick.html#misc">Miscellaneous Methods</a> for a list of these methods.</p>
<p>Once you are finished with a PerlMagick object you should consider destroying it. Each image in an image sequence is stored in virtual memory. This can potentially add up to mebibytes of memory. Upon destroying a PerlMagick object, the memory is returned for use by other Perl methods. The recommended way to destroy an object is with <code>undef</code>:</p>
<preclass="highlight"><code>undef $image;
</code></pre>
<p>To delete all the images but retain the <code>Image::Magick</code> object use</p>
<preclass="highlight"><code>@$image = ();
</code></pre>
<p>and finally, to delete a single image from a multi-image sequence, use</p>
<p>The next section illustrates how to use various PerlMagick methods to manipulate an image sequence.</p>
<p>Some of the PerlMagick methods require external programs such as <ahref="http://www.cs.wisc.edu/~ghost/">Ghostscript</a>. This may require an explicit path in your PATH environment variable to work properly. For example (in Unix),</p>
<p>The script reads three images, crops them, and writes a single image as a GIF animation sequence. In many cases you may want to access individual images of a sequence. The next example illustrates how this done:</p>
<h2><aclass="anchor"id="read"></a>Read or Write an Image</h2>
<p>Use the methods listed below to either read, write, or display an image or image sequence:</p>
<tableclass="table table-sm table-hover">
<caption>Read or Write Methods</caption>
<colgroup>
<colwidth="20%"/>
<colwidth="20%"/>
<colwidth="20%"/>
<colwidth="40%"/>
</colgroup>
<tbody>
<tr>
<th>Method</th>
<th>Parameters</th>
<th>Return Value</th>
<th>Description</th>
</tr>
<tr>
<td>Read</td>
<td>one or more filenames</td>
<td>the number of images read</td>
<td>read an image or image sequence</td>
</tr>
<tr>
<td>Write</td>
<td>filename</td>
<td>the number of images written</td>
<td>write an image or image sequence</td>
</tr>
<tr>
<td>Display</td>
<td>server name</td>
<td>the number of images displayed</td>
<td>display the image or image sequence to an X server</td>
</tr>
<tr>
<td>Animate</td>
<td>server name</td>
<td>the number of images animated</td>
<td>animate image sequence to an X server</td>
</tr>
</tbody>
</table>
<p>For convenience, methods Write(), Display(), and Animate() can take any parameter that <ahref="perl-magick.html#set-attribute">SetAttribute</a> knows about. For example,</p>
<p>Note, reading from or writing to a Perl filehandle may fail under Windows due to different versions of the C-runtime libraries between ImageMagick and the ActiveState Perl distributions or if one of the DLL's is linked with the /MT option. See <ahref="http://msdn.microsoft.com/en-us/library/ms235460.aspx">Potential Errors Passing CRT Objects Across DLL Boundaries</a> for an explanation.</p>
<p>If <code>%0Nd, %0No, or %0Nx</code> appears in the filename, it is interpreted as a printf format specification and the specification is replaced with the specified decimal, octal, or hexadecimal encoding of the scene number. For example,</p>
<p>You can optionally add <i>Image</i> to any method name. For example, ReadImage() is an alias for method Read().</p>
<h2><aclass="anchor"id="manipulate"></a>Manipulate an Image</h2>
<p>Once you create an image with, for example, method ReadImage() you may want to operate on it. Below is a list of all the image manipulations methods available to you with PerlMagick. There are <ahref="examples.html">examples</a> of select PerlMagick methods. Here is an example call to an image manipulation method:</p>
<td>set each pixel whose value is below zero to zero and any the pixel whose value is above the quantum range to the quantum range (e.g. 65535) otherwise the pixel value remains unchanged.</td>
<td>apply color correction to the image. Although you can use variable sized matrices, typically you use a 5 x 5 for an RGBA image and a 6x6 for CMYKA. A 6x6 matrix is required for offsets (populate the last column with normalized values).</td>
<td>compares each image with the next in a sequence and returns the minimum bounding region of any pixel differences it discovers. Images do not have to be the same size, though it is best that all the images are coalesced (images are all the same size, on a flattened canvas, so as to represent exactly how a specific frame should look).</td>
<td>apply a convolution kernel to the image. Given a kernel <i>order</i> , you would supply <i>order*order</i> float values (e.g. 3x3 implies 9 values).</td>
<td>copy pixels from the image as defined by the <code>width</code>x<code>height</code>+<code>x</code>+<code>y</code> to image at offset +<code>dx</code>,+<code>dy</code>.</td>
<td>changes the color value of any pixel that matches the color of the target pixel and is a neighbor. If you specify a border color, the color value is changed for any neighbor pixel that is not that color.</td>
</tr>
<tr>
<td>ForwardFourierTransform</td>
<td>magnitude=>{True, False}</td>
<td>implements the forward discrete Fourier transform (DFT)</td>
<td>compare each image the GIF disposed forms of the previous image in the sequence. From this, attempt to select the smallest cropped image to replace each frame, while preserving the results of the animation.</td>
</tr>
<tr>
<td>Level</td>
<td>levels=><i>string</i>, 'black-point'=><i>double</i>, 'gamma'=><i>double</i>, 'white-point'=><i>double</i>, channel=>{Red, RGB, All, etc.}</td>
<td>adjust the level of image contrast</td>
</tr>
<tr>
<td>LevelColors</td>
<td>invert=>>{True, False}, 'black-point'=><i>string</i>, 'white-point'=><i>string</i>, channel=>{Red, RGB, All, etc.}</td>
<td>changes the matte value of any pixel that matches the color of the target pixel and is a neighbor. If you specify a border color, the matte value is changed for any neighbor pixel that is not that color.</td>
<td>reduce image noise and reduce detail levels with a Gaussian operator of the given radius and standard deviation (sigma) at the given angle to simulate the effect of motion</td>
<td>epsilon=><i>double</i>, channel=>{Red, RGB, All, etc.}</td>
<td>set each pixel whose value is less than |<var>epsilon</var>| to <var>-epsilon</var> or <var>epsilon</var> (whichever is closer) otherwise the pixel value remains unchanged..</td>
<td>shear the image along the X or Y axis by a positive or negative shear angle</td>
</tr>
<tr>
<td>SigmoidalContrast</td>
<td>geometry=><i>string</i>, 'contrast'=><i>double</i>, 'mid-point'=><i>double</i> channel=>{Red, RGB, All, etc.}, sharpen=>{True, False}</td>
<td>sigmoidal non-lineraity contrast control. Increase the contrast of the image using a sigmoidal transfer function without saturating highlights or shadows. <var>Contrast</var> indicates how much to increase the contrast (0 is none; 3 is typical; 20 is a lot); <var>mid-point</var> indicates where midtones fall in the resultant image (0 is white; 50% is middle-gray; 100% is black). To decrease contrast, set sharpen to False.</td>
</tr>
<tr>
<td>Signature</td>
<td><br/></td>
<td>generate an SHA-256 message digest for the image pixel stream</td>
<td>force all pixels above the threshold intensity into white</td>
</tr>
</tbody>
</table>
<p>Note, that the <code>geometry</code> parameter is a short cut for the <code>width</code> and <code>height</code> parameters (e.g. <code>geometry=>'106x80'</code> is equivalent to <code>width=>106, height=>80</code> ).</p>
<p>You can specify <code>@filename</code> in both Annotate() and Draw(). This reads the text or graphic primitive instructions from a file on disk. For example,</p>
<p>The <i>text</i> parameter for methods, Annotate(), Comment(), Draw(), and Label() can include the image filename, type, width, height, or other image attribute by embedding these special format characters:</p>
<p>produces an annotation of <b>MIFF:bird.miff 512x480</b> for an image titled <b>bird.miff</b> and whose width is 512 and height is 480.</p>
<p>You can optionally add <i>Image</i> to any method name. For example, TrimImage() is an alias for method Trim().</p>
<p>Most of the attributes listed above have an analog in <ahref="convert.html">convert</a>. See the documentation for a more detailed description of these attributes.</p>
<h2><aclass="anchor"id="set-attribute"></a>Set an Image Attribute</h2>
<p>Use method Set() to set an image attribute. For example,</p>
<td>print detailed information about the image</td>
</tr>
<tr>
<td>virtual-pixel</td>
<td>{Background Black Constant Dither Edge Gray Mirror Random Tile Transparent White}</td>
<td>the virtual pixel method</td>
</tr>
<tr>
<td>white-point</td>
<td><i>x-value</i>, <i>y-value</i></td>
<td>chromaticity white point (e.g. 0.3127, 0.329)</td>
</tr>
</tbody>
</table>
<p>Note, that the <code>geometry</code> parameter is a short cut for the <code>width</code> and <code>height</code> parameters (e.g. <code>geometry=>'106x80'</code> is equivalent to <code>width=>106, height=>80</code>).</p>
<p>SetAttribute() is an alias for method Set().</p>
<p>Most of the attributes listed above have an analog in
<ahref="convert.html">convert</a>. See the documentation for a more detailed description of these attributes.</p>
<h2><aclass="anchor"id="get-attribute"></a>Get an Image Attribute</h2>
<p>Use method Get() to get an image attribute. For example,</p>
<p>In addition to all the attributes listed in <ahref="perl-magick.html#set-attribute">Set an Image Attribute</a> , you can get these additional attributes:</p>
<td>user time in seconds since the image was created</td>
</tr>
<tr>
<td>version</td>
<td><i>string</i></td>
<td>get PerlMagick's version</td>
</tr>
<tr>
<td>width</td>
<td><i>integer</i></td>
<td>the number of columns or width of an image</td>
</tr>
<tr>
<td>XMP</td>
<td><i>string</i></td>
<td>XMP profile</td>
</tr>
<tr>
<td>x-resolution</td>
<td><i>integer</i></td>
<td>x resolution of the image</td>
</tr>
<tr>
<td>y-resolution</td>
<td><i>integer</i></td>
<td>y resolution of the image</td>
</tr>
</tbody>
</table>
<p>GetAttribute() is an alias for method Get().</p>
<p>Most of the attributes listed above have an analog in
<ahref="convert.html">convert</a>. See the documentation for a more detailed description of these attributes.</p>
<h2><aclass="anchor"id="compare"></a>Compare an Image to its Reconstruction</h2>
<p>Mathematically and visually annotate the difference between an image and its reconstruction with the Compare() method. The method supports these parameters:</p>
<tableclass="table table-sm table-hover">
<caption>Compare Parameters</caption>
<tbody>
<tr>
<th>Parameter</th>
<thstyle="width: 40%">Values</th>
<thstyle="width: 40%">Description</th>
</tr>
<tr>
<td>channel</td>
<td><i>double</i></td>
<td>select image channels, the default is all channels except alpha.</td>
</tr>
<tr>
<td>fuzz</td>
<td><i>double</i></td>
<td>colors within this distance are considered equal</td>
</tr>
<tr>
<td>image</td>
<td><i>image-reference</i></td>
<td>the image reconstruction</td>
</tr>
<tr>
<td>metric</td>
<td>AE, MAE, MEPP, MSE, PAE, PSNR, RMSE</td>
<td>measure differences between images with this metric</td>
</tr>
</tbody>
</table>
<p>In this example, we compare the ImageMagick logo to a sharpened reconstruction:</p>
<p>In addition to the reported root mean squared error of around 0.024, a difference image is displayed so you can visually identify the difference between the images.</p>
<h2><aclass="anchor"id="montage"></a>Create an Image Montage</h2>
<p>Use method Montage() to create a composite image by combining several separate images. The images are tiled on the composite image with the name of the image optionally appearing just below the individual tile. For example,</p>
<td>surround the image with an ornamental border</td>
</tr>
<tr>
<td>geometry</td>
<td><i>geometry</i></td>
<td>preferred tile and border size of each tile of the composite
image (e.g. 120x120+4+3>)</td>
</tr>
<tr>
<td>gravity</td>
<td>NorthWest, North, NorthEast, West, Center, East, SouthWest,
South, SouthEast</td>
<td>direction image gravitates to within a tile</td>
</tr>
<tr>
<td>label</td>
<td><i>string</i></td>
<td>assign a label to an image</td>
</tr>
<tr>
<td>mode</td>
<td>Frame, Unframe, Concatenate</td>
<td>thumbnail framing options</td>
</tr>
<tr>
<td>pointsize</td>
<td><i>integer</i></td>
<td>pointsize of the Postscript or TrueType font</td>
</tr>
<tr>
<td>shadow</td>
<td>{True, False}</td>
<td>add a shadow beneath a tile to simulate depth</td>
</tr>
<tr>
<td>stroke</td>
<td><ahref="color.html">color name</a></td>
<td>stroke color for annotations</td>
</tr>
<tr>
<td>texture</td>
<td><i>string</i></td>
<td>name of texture to tile onto the image background</td>
</tr>
<tr>
<td>tile</td>
<td><i>geometry</i></td>
<td>the number of tiles per row and page (e.g. 6x4)</td>
</tr>
<tr>
<td>title</td>
<td>string</td>
<td>assign a title to the image montage</td>
</tr>
<tr>
<td>transparent</td>
<td><i>string</i></td>
<td>make this color transparent within the image</td>
</tr>
</tbody>
</table>
<p>Note, that the <code>geometry</code> parameter is a short cut for the <code>width</code> and <code>height</code> parameters (e.g. <code>geometry=>'106x80'</code> is equivalent to <code>width=>106, height=>80</code>).</p>
<p>MontageImage() is an alias for method Montage().</p>
<p>Most of the attributes listed above have an analog in <ahref="montage.html">montage</a>. See the documentation for a more detailed description of these attributes.</p>
<h2><aclass="anchor"id="blobs"></a>Working with Blobs</h2>
<p>A blob contains data that directly represent a particular image
format in memory instead of on disk. PerlMagick supports
blobs in any of these image <ahref="formats.html">formats</a> and provides methods to convert a blob to or from a particular image format.</p>
<td>an array of image data in the respective image format</td>
<td>convert an image or image sequence to an array of blobs</td>
</tr>
<tr>
<td>BlobToImage</td>
<td>one or more blobs</td>
<td>the number of blobs converted to an image</td>
<td>convert one or more blobs to an image</td>
</tr>
</tbody>
</table>
<p>ImageToBlob() returns the image data in their respective formats. You can then print it, save it to an ODBC database, write it to a file, or pipe it to a display program:</p>
<p>appends all the images associated with object <code>$image</code>. By default, images are stacked left-to-right. Set <code>stack</code> to True to stack them top-to-bottom.</p>
<p>The Clone() method copies a set of images. For example,</p>
<preclass="highlight"><code>$q = $p->Clone();
</code></pre>
<p>copies all the images from object <code>$p</code> to <code>$q</code>. You can use this method for single or multi-image sequences.</p>
<p>Coalesce() composites a set of images while respecting any page
offsets and disposal methods. GIF, MIFF, and MNG animation sequences
typically start with an image background and each subsequent image
varies in size and offset. A new image sequence is returned with all
images the same size as the first images virtual canvas and composited
with the next image in the sequence.. For example,</p>
<p>averages all the images associated with object <code>$image</code>.</p>
<p>The Features() method returns features for each channel in the image in each of four directions (horizontal, vertical, left and right diagonals) for the specified distance. The features include the angular second momentum, contrast, correlation, sum of squares: variance, inverse difference moment, sum average, sum varience, sum entropy, entropy, difference variance, difference entropy, information measures of correlation 1, information measures of correlation 2, and maximum correlation coefficient. Values in RGB, CMYK, RGBA, or CMYKA order (depending on the image type).</p>
<p>replaces the red channel with the average of the green and blue channels.</p>
<p>See <ahref="fx.html">FX, The Special Effects Image Operator</a> for a detailed discussion of this method.</p>
<p>Histogram() returns the unique colors in the image and a count for each one. The returned values are an array of red, green, blue, opacity, and count values.</p>
<p>The Morph() method morphs a set of images. Both the image pixels and size are linearly interpolated to give the appearance of a meta-morphosis from one image to the next:</p>
<p>where <i>frames</i> is the number of in-between images to generate. The default is 1.</p>
<p>Mosaic() creates an mosaic from an image sequence.</p>
<p>Method Mogrify() is a single entry point for the image manipulation methods (<ahref="perl-magick.html#manipulate">Manipulate an Image</a>). The parameters are the name of a method followed by any parameters the method may require. For example, these calls are equivalent:</p>
<p>Method MogrifyRegion() applies a transform to a region of the image. It is similar to Mogrify() but begins with the region geometry. For example, suppose you want to brighten a 100x100 region of your image at location (40, 50):</p>
<p>Ping() is a convenience method that returns information about an image without having to read the image into memory. It returns the width, height, file size in bytes, and the file format of the image. You can specify more than one filename but only one filehandle:</p>
<p>PreviewImage() tiles 9 thumbnails of the specified image with an image processing operation applied at varying strengths. This may be helpful pin-pointing an appropriate parameter for a particular image processing operation. Choose from these operations: <code>Rotate, Shear, Roll, Hue, Saturation, Brightness, Gamma, Spiff, Dull, Grayscale, Quantize, Despeckle, ReduceNoise, AddNoise, Sharpen, Blur, Threshold, EdgeDetect, Spread, Solarize, Shade, Raise, Segment, Swirl, Implode, Wave, OilPaint, CharcoalDrawing, JPEG</code>. Here is an example:</p>
<p>Where <i>parameters</i> is any parameter of the <ahref="perl-magick.html#manipulate">Annotate</a> method. The return values are:</p>
<ol>
<li>character width</li>
<li>character height</li>
<li>ascender</li>
<li>descender</li>
<li>text width</li>
<li>text height</li>
<li>maximum horizontal advance</li>
<li>bounds: x1</li>
<li>bounds: y1</li>
<li>bounds: x2</li>
<li>bounds: y2</li>
<li>origin: x</li>
<li>origin: y</li>
</ol>
<p>Use QueryMultilineFontMetrics() to get the maximum text width and height for multiple lines of text.</p>
<p>Call QueryColor() with no parameters to return a list of known colors names or specify one or more color names to get these attributes: red, green, blue, and opacity value.</p>
<p>Call QueryFont() with no parameters to return a list of known fonts or specify one or more font names to get these attributes: font name, description, family, style, stretch, weight, encoding, foundry, format, metrics, and glyphs values.</p>
<p>Call QueryFormat() with no parameters to return a list of known image formats or specify one or more format names to get these attributes: adjoin, blob support, raw, decoder, encoder, description, and module.</p>
<p>Use RemoteCommand() to send a command to an already running <ahref="display.html">display</a> or <ahref="animate.html">animate</a> application. The only parameter is the name of the image file to display or animate.</p>
<p>smushes together all the images associated with object <code>$image</code>. By default, images are smushed left-to-right. Set <code>stack</code> to True to smushed them top-to-bottom.</p>
<p>Statistics() returns the image statistics for each channel in the image. The returned values are an array of depth, minima, maxima, mean, standard deviation, kurtosis, skewness, and entropy values in RGB, CMYK, RGBA, or CMYKA order (depending on the image type).</p>
<p>All PerlMagick methods return an undefined string context upon success. If any problems occur, the error is returned as a string with an embedded numeric status code. A status code less than 400 is a warning. This means that the operation did not complete but was recoverable to some degree. A numeric code greater or equal to 400 is an error and indicates the operation failed completely. Here is how exceptions are returned for the different methods:</p>
<p>Methods which return a number (e.g. Read(), Write()):</p>