Using Image::Magick

Image::Magick provides an all-purpose graphics API for Perl. You manipulate images by reading them into an Image::Magick object and then setting attributes and calling methods on that object. Most of the file formats supported by Image::Magick have certain attributes in common, though each may implement them differently. For example, JPEG and PNG both support compression, but have different compression schemes.

Image::Magick defines a wide range of file attributes, which are listed in tables later in this chapter. Many of the image manipulation functions (as well as Read( ) and Write( )) allow you to set various attributes when calling them, or you can use Set( ) to do it directly, as in:

$image->Set(loop=>100);
$image->[$x]->Set(dither=>1);

Note that you are not manipulating the image simply by setting attributes. In the above examples, setting the loop attribute adds a looping extension to a GIF file when it is written. The second example simply sets the default value of the dither attribute. Setting the attribute doesn’t dither the image, but subsequent operations, such as a call to Quantize( ), are performed using dithering.

Use Get( ) to get an attribute:

($w, $h, $d) = $q->Get('columns', 'rows', 'depth');
$colors = $q->[2]->Get('colors');

The functions GetAttribute( ) and SetAttribute( ) are aliases for Get( ) and Set( ) and may be used interchangeably.

Image::Magick provides a suite of methods for manipulating images. You can optionally add the word Image to ...

Get Perl Graphics Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.