Imager::Cookbook

Imager::Cookbook contains recipes for working with Imager.
Download

Imager::Cookbook Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Tony Cook
  • Publisher web site:
  • http://search.cpan.org/~tonyc/

Imager::Cookbook Tags


Imager::Cookbook Description

Imager::Cookbook contains recipes for working with Imager. Imager::Cookbook contains recipes for working with Imager.FILESThis is described in detail in Imager::Files.Reading an image from a file my $image = Imager->new; $image->read(file=>$filename) or die $image->errstr;See Imager::Files.Writing an image to a file $image->write(file=>$filename) or die $image->errstr;Write an animated gif. # build an array of images to use in the gif my @images; # synthesize the images or read them from files, it doesn't matter ... # write the gif Imager->write_multi({ file=>$filename, type=>'gif' }, @images) or die Imager->errstr;See "Writing an animated GIF" in Imager::Files for a more detailed example.Reading multiple images from one fileSome formats, like GIF and TIFF support multiple images per file. Use the read_multi() method to read them: my @images = Imager->read_multi(file=>$filename) or die Imager->errstr;Converting from one file format to anotherThis is as simple as reading the original file and writing the new file, for single images: my $image = Imager->new; # Imager auto-detects the input file type $image->read(file => $input_filename) or die $image->errstr; # Imager derives the output file format from the filename $image->write(file => $output_filename) or die $image->errstr; # or you can supply a type parameter: $image->write(file => $output_filename, type => 'gif') or die $image->errstr;The main issue that can occur with this is if the input file has transparency and the output file format doesn't support that. This can be a problem when converting from GIFs to JPEGs for example.To work around that you can compose the source image onto a background color: if ($image->getchannels == 4 or $image->getchannels == 2) { my $back = Imager->new(xsize => $image->getwidth, ysize => $image->getheight); # grey background for grayscale images, red for color my $back_color = $image->getchannels == 2 ? : 'red'; $back->box(filled => 1, color => $back_color); $back->rubthrough(src => $image); $image = $back; } # now we can write safely to jpeg or pnmSome formats support multiple files, so if you want to convert from say tiff to jpeg, you'll need multiple output files: my @images = Imager->read_multi(file => 'input.tif') or die Imager->errstr; my $index = 1; for my $image (@images) { $image->write(file => sprintf('outputd.jpg', $index++)) or die $image->errstr; }Requirements:· Perl Requirements: · Perl


Imager::Cookbook Related Software