Overview of graphics file formats
2.3 C ONVERSION OF GRAPHICS FORMATS
To convert graphics between formats it is best to use the Image::Magick module. This module can read and write a wide variety of image formats, sometimes with the help of external tools and libraries. Some of the formats that can be read by Image::Magick are listed in table 2.1.
Table 2.1 A partial list of image formats recognized by Image::Magick. Some of these formats are parsed with the help of external modules or programs which require you to install these before compiling Image::Magick. See the documentation for a full list.
Format Description
BMP, BMP24 MS Windows bitmap image file
CGM Computer Graphics Metafile
CMYK Raw cyan, magenta, yellow, and black bytes EPI, EPS, EPSF Encapsulated PostScript Formats
GIF CompuServe graphics interchange format HTML HTML with a client-side image map JPEG Joint Photographic Experts Group JFIF
MIFF Magick image file format
MNG Multiple-image Network Graphics
PBM, PGM, PNM, PPM Portable Xmap format
PCD, PCDS Photo CD
PICT Apple Macintosh QuickDraw/PICT file
PNG Portable Network Graphics
PSD Adobe Photoshop bitmap file
TGA Truevision Targa image file
TIFF Tagged Image File Format
WMF Windows Meta File
XBM, XPM X Windows system bitmap/pixmap
For a complete list, see for example the ImageMagick web site [17] or the documen-tation for the command line tool convert. In addition to standard external image for-mats, Image::Magick has some built-in format specifiers, which are listed in table 2.2.
When reading a file, Image::Magick will usually be able to guess the file type by its sig-nature.8 In the rare cases in which that doesn’t work, you may specify the file format in the file name you give to the Read() method. The Write() method can be used to save images to disk. It uses the extension of the file name specified to determine the format to save. If the file extension cannot be used, or you want to override it, you may specify a format explicitly (see the code examples below). If neither a file extension or explicit format are specified, the image attribute magick will be used to determine the format of the file to save. We will discuss some concrete examples after showing the code that you will find at the start of almost any Perl program using Image::Magick:
use Image::Magick;
my $rc;
my $img = Image::Magick->new();
We read in a GIF format file, and save it as a PNG format file.
$rc = $img->Read('file.gif');
warn $rc if $rc;
$rc = $img->Write('file.png');
warn $rc if $rc;
Table 2.2 Some of the internal special image formats of Image::Magick. These formats are not really image formats, but are special instructions to the ImageMagick engine to create an image with certain characteristics.
Specifiers Description
GRADIENT Gradual change from one shade to another GRANITE Granite texture
GRAY Raw gray bytes
HISTOGRAM Histogram of the image LABEL Create a text image NETSCAPE Netscape 6x6x6 color cube PLASMA plasma fractal image VID Visual Image Directory XC Filled with a single color
8 Most, if not all, graphics file formats can be identified by looking at the first few bytes of the file. Each format has its own characteristic sequence of bytes, called the signature. See the definition of png_size() and xcf_size() in the previous section for examples of this.
Then we create a white tile of 70 by 70 pixels, and save it as a TIFF format file.
@$img = ();
$img->Set(size => '70x70');
$rc = $img->Read('xc:white');
warn $rc if $rc;
$rc = $img->Write('whiteTile.tiff');
warn $rc if $rc;
In the preceding code you might have noticed the @$img = (). That is the best way to remove all the images from the $img object, without actually destroying the object itself. If this were not done, subsequent reads would create a multi-image object (see also chapter 7).
To create an image with a gradient going from light to dark gray the following code can be used:
@$img = ();
$img->Set(size => '100x600');
$rc = $img->Read('gradient:#efefef-#3f3f3f');
warn $rc if $rc;
$img->Rotate(degrees => -90);
$rc = $img->Write('grayGradient.jpg');
warn $rc if $rc;
Image::Magick gradients always run from top to bottom, so to attain a gradient that changes horizontally, we rotate the created image by 90 degrees. The result can be seen in figure 2.1.
2.4
SUMMARYIn this chapter some image formats and methods to obtain information about the image size and format were discussed, and the conversion of one image format to another with the help of Image::Magick was demonstrated. This is hopefully sufficient to provide a solid base from which to work with image files and the various formats that are available.
SEE ALSO The Image::Magick documentation and the reference in appendix A should be consulted for more information on the various Image::Magick methods discussed. The Image::Size module comes equipped with useful documenta-tion which is recommended reading for anyone wishing to use the module.
Figure 2.1 An illustration of Image::Magick’s built-in type gradient, rotated and converted to a JPEG image.
C H A P T E R 3