Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
Imager - Convert Image File Format
This section provides a tutorial example on how to convert image file from one format to another format using read() and write() functions from the Perl Imager module.
With the Imager module, you can read an image from one file format and write it to a different file format using read() and write() functions.
1. Write a Perl script to read an image from an image file and write it out in a different file format specified in the file name extension:
#- Imager-Convert-Format.pl #- Copyright (c) HerongYang.com. All Rights Reserved. use Imager; my ($file, $format) = @ARGV; $file = "sample.bmp" unless $file; $format = "bmp" unless $format; $img = Imager->new(); $img->read(file=>$file) or die $img->errstr; print "Image created from:\n"; print " File: $file\n"; print " Width: ", $img->getwidth(), "\n"; print " Height: ", $img->getheight(), "\n"; $file = "Imager-Converted.$format"; $img->write(file=>$file) or die $img->errstr; print "Image saved:\n"; print " File: $file\n";
2. Run the above script to convert an image file from BMP format to ICO format:
herong$ perl Imager-Convert-Format.pl sample.bmp ico Image created from: File: sample.bmp Width: 16 Height: 16 Image saved: File: Imager-Converted.ico
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Directories and Read File Names
File System Functions and Operations
Imager - Create and Save Image
Imager - Draw Graphical Elements
►Imager - Convert Image File Format
Imager::File::PNG - PNG File Format
Install LIBPNG from Source Code
Install Imager::File::PNG Manually
Install PerlMagick from Source Code
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary