1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
=head1 NAME
Imager::Tutorial - an introduction to Imager.
=head1 DESCRIPTION
=head2 Before you start
If you have the necessary knowledge, install the image format
libraries you want Imager image file support for, and Imager itself,
otherwise arrange to have it done.
You will also want some sort of image viewer tool, whether an image
editor like Photoshop or the GIMP, or a web browser.
=head2 Hello Boxes! - A Simple Start
As with any perl program it's useful to start with a #! line, and to
enable strict mode:
#!/usr/bin/perl -w
# you might to 'use warnings;' instead of the -w above
use strict;
These lines will be omitted in further examples.
As with any module, you need to load it:
use Imager;
Now create a image to draw on:
my $image = Imager->new(xsize => 100, ysize => 100);
and draw a couple of filled rectangles on it:
$image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
filled => 1, color => 'blue');
$image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
filled => 1, color => 'green');
Since the first box fills the whole image, it can be simplified to:
$image->box(filled => 1, color => 'blue');
and save it to a file:
$image->write(file=>'tutorial1.ppm')
or die 'Cannot save tutorial1.ppm: ', $image->errstr;
So our completed program is:
use Imager;
my $image = Imager->new(xsize => 100, ysize => 100);
$image->box(filled => 1, color => 'blue');
$image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
filled => 1, color => 'green');
$image->write(file=>'tutorial1.ppm')
or die 'Cannot save tutorial1.ppm: ', $image->errstr;
=head2 Adding some text
The first thing you need to draw text is a font object:
# use a different file, depending on the font support you have in
# your installed Imager.
my $font_filename = 'fontfiles/ImUgly.ttf';
my $font = Imager::Font->new(file=>$font_filename)
or die "Cannot load $font_filename: ", Imager->errstr;
If you're on Windows, you can supply a face name instead:
my $font = Imager::Font->new(face=>'Arial Bold')
or die "Cannot load 'Arial Bold: ", Imager->errstr;
and draw the text:
my $text = "Hello Boxes!";
my $text_size = 12;
$font->align(string => $text,
size => $text_size,
color => 'red',
x => $image->getwidth/2,
y => $image->getheight/2,
halign => 'center',
valign => 'center',
image => $image);
So inserting this into our existing code we have:
use Imager;
my $image = Imager->new(xsize => 100, ysize => 100);
$image->box(xmin => 0, ymin => 0, xmax => 99, ymax => 99,
filled => 1, color => 'blue');
$image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
filled => 1, color => 'green');
# use a different file, depending on the font support you have in
# your installed Imager.
my $font_filename = 'fontfiles/ImUgly.ttf';
my $font = Imager::Font->new(file=>$font_filename)
or die "Cannot load $font_filename: ", Imager->errstr;
my $text = "Hello Boxes!";
my $text_size = 12;
$font->align(string => $text,
size => $text_size,
color => 'red',
x => $image->getwidth/2,
y => $image->getheight/2,
halign => 'center',
valign => 'center',
image => $image);
$image->write(file=>'tutorial2.ppm')
or die 'Cannot save tutorial2.ppm: ', $image->errstr;
=head2 Using an existing image as a base
To load an image from a file, first create an empty image object:
my $read_image = Imager->new;
then call the read method:
my $image_source = shift; # from the command-line
$read_image->read(file=>$image_source)
or die "Cannot load $image_source: ", $image->errstr;
To keep to our working size, we'll scale the image:
# the scale() method always does a proportional scale, we don't want
# that here
my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
draw our inner box on that, and save the result:
$scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
filled => 1, color => 'green');
$scaled_image->write(file=>'tutorial3.ppm')
or die 'Cannot save tutorial3.ppm: ', $image->errstr;
so the complete program is:
use Imager;
my $read_image = Imager->new;
my $image_source = shift; # from the command-line
$read_image->read(file=>$image_source)
or die "Cannot load $image_source: ", $image->errstr;
# the scale() method always does a proportional scale, we don't want
# that here
my $scaled_image = $read_image->scaleX(pixels=>100)->scaleY(pixels=>100);
$scaled_image->box(xmin => 20, ymin => 20, xmax => 79, ymax => 79,
filled => 1, color => 'green');
$scaled_image->write(file=>'tutorial3.ppm')
or die 'Cannot save tutorial3.ppm: ', $image->errstr;
=head1 AUTHOR
Tony Cook <tony@imager.perl.org>
=head1 REVISION
$Revision: 811 $
=cut
|