Book HomeMastering Perl/TkSearch this book

Chapter 17. Images and Animations


An Overview of Perl/Tk Image Types
Methods Common to All Image Types
Bitmap Primitives
The Bitmap Image Type
The Pixmap Image Type
The Photo Image Type
The Compound Image Type
tkneko—Animating the Neko on a Canvas
Tile and Transparent Images
Miscellaneous Image Methods
Simple Photo Rotations

In its early days, Tk pictures were limited to X11 bitmaps (XBMs), a two-color text-based format that let us enhance Labels and Buttons with simple drawings in place of plain text strings. XBM support still exists, but these days, we think in terms of images. Perl/Tk supports numerous image types, which may be further classified into distinct image formats. An image is an object in its own right, meaning it must be created before it can be used, has methods that manipulate it, and should be destroyed when we are through with it.

This chapter discusses the various image flavors and how we might use them, either as static pictures or dynamic animations.

17.1. An Overview of Perl/Tk Image Types

Perl/Tk supports the Bitmap, Pixmap, and Photo image types, illustrated in Figure 17-1.[42]

[42] There's also a new Tix image type called the Compound type that we'll examine in a later section.

Figure 17-1

Figure 17-1. The three Perl/Tk image types

The Bitmap image type (column one) handles XBM files commonly found on Unix systems. XBM files are usually used for desktop icons and cursor shapes and are actually C language statements that define a two-dimensional array of source bits. In their simplest forms, an "on" source bit displays a foreground color and an "off" source bit displays a background color. There is an optional bitmap array of mask bits. If a mask is specified, pixels where the mask is zero display nothing, producing a transparent effect by allowing the background to show through. If the mask bit is one, the pixel displays the foreground color if the source bit is one and the background color if the source bit is zero.

The Pixmap image type (column two) handles X11 pixmap (XPM) files. XPM files are also text files of C language statements and are suitable for colored icons and cursor shapes. This format uses ASCII characters to define a color lookup table, then encodes the picture as a series of C strings containing characters from the color table. Each string represents a row of the picture and each character of the string a pixel from that row.

The most sophisticated image type, Photo, handles various image formats. The default Perl/Tk Photo formats are shown in Figure 17-1, column three. They include Win32 bitmaps (BMP), Unix portable pixmaps (PPM), and graphic interchange format (GIF) files. Available on CPAN are separately bundled modules (column four) for handling "ping" (PNG), joint photographic experts group (JPEG), and tagged image format (TIFF) files. You can find PNG, JPEG, and TIFF image format modules at

All these image formats consist of binary data. We'll see the significance of this in Section 17.7, "The Photo Image Type".

Unlike Bitmap and Pixmap images, Photos have many methods that can manipulate the image, such as reading, writing, and copying ranges of pixels.

Column five shows that the Photo image type can even handle XBM and XPM files, although the Photo versions of these image formats are considerably larger than the native formats. Additionally, Photo supports many options that Bitmap and Pixmap do not, and vice versa, so you really don't want to do this.

Here's the program that generated Figure 17-1. Notice the Photo image formats JPEG, PNG, and TIFF—not part of the Perl/Tk distribution—must be specifically imported. It's easy to forget this, and if you do, Tk won't find the proper image handler and will display "couldn't recognize data in image file."

The foreach statement loops over a list of lists: a list of anonymous arrays, each of which is a list of strings. The first string element is an image type and the remaining elements are the image formats supported by that image type (they're actually common three-letter "file extensions").

All the widgets for each image type are contained in a Frame gridded at row zero of successive columns. Each column is labeled with its image type. The while block creates an image instance for each image format supported by the image type and displays it in a Label widget. Notice that Perl allows us to store the image constructor name in a variable, $image_type. The -file option specifies the name of the image file; in this case, a picture of a neko.[43]

[43] Neko is Japanese for "cat." We'll see the neko throughout this chapter.

#!/usr/local/bin/perl -w
use Tk;
use Tk::widgets qw/JPEG PNG TIFF/;
use strict;

my $mw = MainWindow->new;
my $column = 0;

foreach (
        [qw/Bitmap xbm/],
        [qw/Pixmap xpm/],
        [qw/Photo  bmp ppm gif/], 
        [qw/Photo  png jpg tif/],
        [qw/Photo  xbm xpm/],
        ) {

    my $image_type = shift @$_;
    my $f = $mw->Frame->grid(-row => 0, -column => $column++, -sticky => 'n');
    my $l = $f->Label(-text => $image_type, -foreground => 'blue')->grid;

    while (my $image_format = shift @$_) {
        my $image = $mw->$image_type(-file => "Icon.${image_format}");
        $f->Label(-image => $image)->grid;
        $f->Label(-text  => $image_format)->grid;

} # forend all image types


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.