3 Imager::Tutorial - an introduction to Imager.
7 =head2 Before you start
9 If you have the necessary knowledge, install the image format
10 libraries you want Imager image file support for, and Imager itself,
11 otherwise arrange to have it done.
13 You will also want some sort of image viewer tool, whether an image
14 editor like Photoshop or the GIMP, or a web browser.
16 =head2 Hello Boxes! - A Simple Start
18 As with any perl program it's useful to start with a #! line, and to
22 # you might to 'use warnings;' instead of the -w above
25 These lines will be omitted in further examples.
27 As with any module, you need to load it:
31 Now create a image to draw on:
33 my $image = Imager->new(xsize => 100, ysize => 100);
35 and draw a couple of filled rectangles on it:
37 $image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
38 filled => 1, color => 'blue');
39 $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
40 filled => 1, color => 'green');
42 Since the first box fills the whole image, it can be simplified to:
44 $image->box(filled => 1, color => 'blue');
46 and save it to a file:
48 $image->write(file=>'tutorial1.ppm')
49 or die 'Cannot save tutorial1.ppm: ', $image->errstr;
51 So our completed program is:
55 my $image = Imager->new(xsize => 100, ysize => 100);
57 $image->box(filled => 1, color => 'blue');
58 $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
59 filled => 1, color => 'green');
61 $image->write(file=>'tutorial1.ppm')
62 or die 'Cannot save tutorial1.ppm: ', $image->errstr;
64 =head2 Adding some text
66 The first thing you need to draw text is a font object:
68 # use a different file, depending on the font support you have in
69 # your installed Imager.
70 my $font_filename = 'fontfiles/ImUgly.ttf';
71 my $font = Imager::Font->new(file=>$font_filename)
72 or die "Cannot load $font_filename: ", Imager->errstr;
74 If you're on Windows, you can supply a face name instead:
76 my $font = Imager::Font->new(face=>'Arial Bold')
77 or die "Cannot load 'Arial Bold: ", Imager->errstr;
81 my $text = "Hello Boxes!";
84 $font->align(string => $text,
87 x => $image->getwidth/2,
88 y => $image->getheight/2,
93 So inserting this into our existing code we have:
97 my $image = Imager->new(xsize => 100, ysize => 100);
99 $image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
100 filled => 1, color => 'blue');
101 $image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
102 filled => 1, color => 'green');
104 # use a different file, depending on the font support you have in
105 # your installed Imager.
106 my $font_filename = 'fontfiles/ImUgly.ttf';
107 my $font = Imager::Font->new(file=>$font_filename)
108 or die "Cannot load $font_filename: ", Imager->errstr;
110 my $text = "Hello Boxes!";
113 $font->align(string => $text,
116 x => $image->getwidth/2,
117 y => $image->getheight/2,
122 $image->write(file=>'tutorial2.ppm')
123 or die 'Cannot save tutorial2.ppm: ', $image->errstr;
125 =head2 Using an existing image as a base
127 To load an image from a file, first create an empty image object:
129 my $read_image = Imager->new;
131 then call the read method:
133 my $image_source = shift; # from the command-line
134 $read_image->read(file=>$image_source)
135 or die "Cannot load $image_source: ", $image->errstr;
137 To keep to our working size, we'll scale the image:
139 # the scale() method always does a proportional scale, we don't want
141 my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
143 draw our inner box on that, and save the result:
145 $scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
146 filled => 1, color => 'green');
148 $scaled_image->write(file=>'tutorial3.ppm')
149 or die 'Cannot save tutorial3.ppm: ', $image->errstr;
151 so the complete program is:
155 my $read_image = Imager->new;
157 my $image_source = shift; # from the command-line
158 $read_image->read(file=>$image_source)
159 or die "Cannot load $image_source: ", $image->errstr;
161 # the scale() method always does a proportional scale, we don't want
163 my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
165 $scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
166 filled => 1, color => 'green');
168 $scaled_image->write(file=>'tutorial3.ppm')
169 or die 'Cannot save tutorial3.ppm: ', $image->errstr;
174 Tony Cook <tony@imager.perl.org>