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:

#- Copyright (c) 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 sample.bmp ico

Image created from:
   File: sample.bmp
   Width:  16
   Height: 16
Image saved:
   File: Imager-Converted.ico

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 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 Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

Image and Picture Processing

 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

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 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 Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB