3 Imager::Files - working with image files
8 $img->write(file=>$filename, type=>$type)
9 or die "Cannot write: ",$img->errstr;
12 $img->read(file=>$filename, type=>$type)
13 or die "Cannot read: ", $img->errstr;
15 Imager->write_multi({ file=> $filename, ... }, @images)
16 or die "Cannot write: ", Imager->errstr;
18 my @imgs = Imager->read_multi(file=>$filename)
19 or die "Cannot read: ", Imager->errstr;
23 You can read and write a variety of images formats, assuming you have
24 the appropriate libraries, and images can be read or written to/from
25 files, file handles, file descriptors, scalars, or through callbacks.
27 To see which image formats Imager is compiled to support the following
28 code snippet is sufficient:
31 print join " ", keys %Imager::formats;
33 This will include some other information identifying libraries rather
36 Reading writing to and from files is simple, use the C<read()>
37 method to read an image:
39 my $img = Imager->new;
40 $img->read(file=>$filename, type=>$type)
41 or die "Cannot read $filename: ", $img->errstr;
43 and the C<write()> method to write an image:
45 $img->write(file=>$filename, type=>$type)
46 or die "Cannot write $filename: ", $img->errstr;
48 If you're reading from a format that supports multiple images per
49 file, use the C<read_multi()> method:
51 my @imgs = Imager->read_multi(file=>$filename, type=>$type)
52 or die "Cannot read $filename: ", Imager->errstr;
54 and if you want to write multiple images to a single file use the
55 C<write_multi()> method:
57 Imager->write_multi({ file=> $filename, type=>$type }, @images)
58 or die "Cannot write $filename: ", Imager->errstr;
60 If the I<filename> includes an extension that Imager recognizes, then
61 you don't need the I<type>, but you may want to provide one anyway.
62 See L</Guessing types> for information on controlling this
65 When you read an image, Imager may set some tags, possibly including
66 information about the spatial resolution, textual information, and
67 animation information. See L</Tags> for specifics.
69 =head2 Input and output
71 When reading or writing you can specify one of a variety of sources or
78 The C<file> parameter is the name of the image file to be written to
79 or read from. If Imager recognizes the extension of the file you do
80 not need to supply a C<type>.
84 C<fh> is a file handle, typically either returned from
85 C<<IO::File->new()>>, or a glob from an C<open> call. You should call
86 C<binmode> on the handle before passing it to Imager.
90 C<fd> is a file descriptor. You can get this by calling the
91 C<fileno()> function on a file handle, or by using one of the standard
92 file descriptor numbers.
96 When reading data, C<data> is a scalar containing the image file data,
97 when writing, C<data> is a reference to the scalar to save the image
98 file data too. For GIF images you will need giflib 4 or higher, and
99 you may need to patch giflib to use this option for writing.
103 Imager will make calls back to your supplied coderefs to read, write
104 and seek from/to/through the image file.
106 When reading from a file you can use either C<callback> or C<readcb>
107 to supply the read callback, and when writing C<callback> or
108 C<writecb> to supply the write callback.
110 When writing you can also supply the C<maxbuffer> option to set the
111 maximum amount of data that will be buffered before your write
112 callback is called. Note: the amount of data supplied to your
113 callback can be smaller or larger than this size.
115 The read callback is called with 2 parameters, the minimum amount of
116 data required, and the maximum amount that Imager will store in it's C
117 level buffer. You may want to return the minimum if you have a slow
118 data source, or the maximum if you have a fast source and want to
119 prevent many calls to your perl callback. The read data should be
120 returned as a scalar.
122 Your write callback takes exactly one parameter, a scalar containing
123 the data to be written. Return true for success.
125 The seek callback takes 2 parameters, a I<POSITION>, and a I<WHENCE>,
126 defined in the same way as perl's seek function.
128 You can also supply a C<closecb> which is called with no parameters
129 when there is no more data to be written. This could be used to flush
134 =head2 Guessing types
136 Imager uses the code reference in $Imager::FORMATGUESS to guess the
137 file type when you don't supply a C<type>. The code reference is
138 called with a single parameter, the filename of the file. The code
139 reference is only called if a C<file> parameter is supplied to the
142 Return either a valid Imager file type, or undef.
144 # I'm writing jpegs to weird filenames
145 local $Imager::FORMATGUESS = sub { 'jpeg' };
147 =head1 TYPE SPECIFIC INFORMATION
149 The different image formats can write different image type, and some have
150 different options to control how the images are written.
152 =head2 PNM (Portable aNy Map)
154 Imager can write PGM (Portable Gray Map) and PPM (Portable PixMaps)
155 files, depending on the number of channels in the image. Currently
156 the images are written in binary formats. Only 1 and 3 channel images
157 can be written, including 1 and 3 channel paletted images.
159 $img->write(file=>'foo.ppm') or die $img->errstr;
161 Imager can read both the ASCII and binary versions of each of the PBM
162 (Portable BitMap), PGM and PPM formats.
164 $img->read(file=>'foo.ppm') or die $img->errstr;
166 PNM does not support the spatial resolution tags.
170 You can supply a C<jpegquality> parameter (0-100) when writing a JPEG
171 file, which defaults to 75%. Only 1 and 3 channel images
172 can be written, including 1 and 3 channel paletted images.
174 $img->write(file=>'foo.jpg', jpegquality=>90) or die $img->errstr;
176 Imager will read a grayscale JPEG as a 1 channel image and a color
177 JPEG as a 3 channel image.
179 $img->read(file=>'foo.jpg') or die $img->errstr;
181 PNM does not support the spatial resolution tags.
183 =head2 GIF (Graphics Interchange Format)
185 You can supply many different options when writing to a GIF file, and
186 you can write a multi-image GIF file, eg. for animation, with the
187 C<write_multi()> method.
189 These options can be specified when calling write_multi() or when
190 writing a single image with the C<gifquant> option set to 'gen'
192 Note that some viewers will ignore some of these options
193 (C<gif_user_input> in particular).
197 =item gif_each_palette
199 Each image in the gif file has it's own palette if this is non-zero.
200 All but the first image has a local colour table (the first uses the
205 The images are written interlaced if this is non-zero.
209 A reference to an array containing the delays between images, in 1/100
212 If you want the same delay for every frame you can simply set this to
213 the delay in 1/100 seconds.
217 A reference to an array contains user input flags. If the given flag
218 is non-zero the image viewer should wait for input before displaying
223 A reference to an array of image disposal methods. These define what
224 should be done to the image before displaying the next one. These are
225 integers, where 0 means unspecified, 1 means the image should be left
226 in place, 2 means restore to background colour and 3 means restore to
231 A reference to an Imager::Color object, which is the colour to use for
232 the palette entry used to represent transparency in the palette. You
233 need to set the transp option (see L<Quantization options>) for this
238 A reference to an array of references to arrays which represent screen
239 positions for each image.
243 If this is non-zero the Netscape loop extension block is generated,
244 which makes the animation of the images repeat.
246 This is currently unimplemented due to some limitations in giflib.
248 =item gif_eliminate_unused
250 If this is true, when you write a paletted image any unused colors
251 will be eliminated from its palette. This is set by default.
255 When reading a GIF all of the sub-images are combined using the screen
256 size and image positions into one big image, producing an RGB image.
257 This may change in the future to produce a paletted image where possible.
259 When you read a single GIF with C<<$img->read()>> you can supply a
260 reference to a scalar in the C<colors> parameter, if the image is read
261 the scalar will be filled with a reference to an anonymous array of
262 L<Imager::Color> objects, representing the palette of the image. This
263 will be the first palette found in the image. If you want the
264 palettes for each of the images in the file, use C<read_multi()> and
265 use the C<getcolors()> method on each image.
267 GIF does not support the spatial resolution tags.
269 GIF will set the following tags in each image when reading, but does
270 not use them when saving to GIF:
276 the offset of the image from the left of the "screen" ("Image Left
281 the offset of the image from the top of the "screen" ("Image Top Position")
285 non-zero if the image was interlaced ("Interlace Flag")
287 =item gif_screen_width
289 =item gif_screen_height
291 the size of the logical screen ("Logical Screen Width",
292 "Logical Screen Height")
296 Non-zero if this image had a local color map.
300 The index in the global colormap of the logical screen's background
301 color. This is only set if the current image uses the global
304 =item gif_trans_index
306 The index of the color in the colormap used for transparency. If the
307 image has a transparency then it is returned as a 4 channel image with
308 the alpha set to zero in this palette entry. ("Transparent Color Index")
312 The delay until the next frame is displayed, in 1/100 of a second.
317 whether or not a user input is expected before continuing (view dependent)
322 how the next frame is displayed ("Disposal Method")
326 the number of loops from the Netscape Loop extension. This may be zero.
330 the first block of the first gif comment before each image.
334 Where applicable, the ("name") is the name of that field from the GIF89
337 =head2 TIFF (Tagged Image File Format)
339 Imager can write images to either paletted or RGB TIFF images,
340 depending on the type of the source image. Currently if you write a
341 16-bit/sample or double/sample image it will be written as an
342 8-bit/sample image. Only 1 or 3 channel images can be written.
344 If you are creating images for faxing you can set the I<class>
345 parameter set to C<fax>. By default the image is written in fine
346 mode, but this can be overridden by setting the I<fax_fine> parameter
347 to zero. Since a fax image is bi-level, Imager uses a threshold to
348 decide if a given pixel is black or white, based on a single channel.
349 For greyscale images channel 0 is used, for color images channel 1
350 (green) is used. If you want more control over the conversion you can
351 use $img->to_paletted() to product a bi-level image. This way you can
354 my $bilevel = $img->to_paletted(colors=>[ NC(0,0,0), NC(255,255,255) ],
355 make_colors => 'none',
356 translate => 'errdiff',
357 errdiff => 'stucki');
363 If set to 'fax' the image will be written as a bi-level fax image.
367 By default when I<class> is set to 'fax' the image is written in fine
368 mode, you can select normal mode by setting I<fax_fine> to 0.
372 Imager should be able to read any TIFF image you supply. Paletted
373 TIFF images are read as paletted Imager images, since paletted TIFF
374 images have 16-bits/sample (48-bits/color) this means the bottom
375 8-bits are lost, but this shouldn't be a big deal. Currently all
376 direct color images are read at 8-bits/sample.
378 TIFF supports the spatial resolution tags. See the
379 C<tiff_resolutionunit> tag for some extra options.
381 The following tags are set in a TIFF image when read, and can be set
386 =item tiff_resolutionunit
388 The value of the ResolutionUnit tag. This is ignored on writing if
389 the i_aspect_only tag is non-zero.
391 The C<i_xres> and C<i_yres> tags are expressed in pixels per inch no
392 matter tha value of this tag, they will be converted to/from the value
393 stored in the TIFF file.
395 =item tiff_documentname
397 =item tiff_imagedescription
411 =item tiff_hostcomputer
413 Various strings describing the image. tiff_datetime must be formatted
414 as "YYYY:MM:DD HH:MM:SS". These correspond directly to the mixed case
415 names in the TIFF specification. These are set in images read from a
416 TIFF and saved when writing a TIFF image.
422 Imager can write 24-bit RGB, and 8, 4 and 1-bit per pixel paletted
423 Windows BMP files. Currently you cannot write compressed BMP files
426 Imager can read 24-bit RGB, and 8, 4 and 1-bit perl pixel paletted
427 Windows BMP files. There is some support for reading 16-bit per pixel
428 images, but I haven't found any for testing.
430 BMP has no support for multi-image files.
432 BMP files support the spatial resolution tags, but since BMP has no
433 support for storing only an aspect ratio, if C<i_aspect_only> is set
434 when you write the C<i_xres> and C<i_yres> values are scaled so the
437 The following tags are set when you read an image from a BMP file:
441 =item bmp_compression
443 The type of compression, if any. This can be any of the following
454 8-bits/pixel paletted value RLE compression.
458 4-bits/pixel paletted value RLE compression.
460 =item BI_BITFIELDS (3)
466 =item bmp_important_colors
468 The number of important colors as defined by the writer of the image.
488 writing an image from CGI (content type, flush, write to fd)
490 writing an animated gif
492 reading tags after reading an image