Imager - Create and Save Image

This section provides a tutorial example on using stat() to return many statistics of the specified file, including file size, creation time, and last modification time.

"Imager" is a Perl module for creating and altering 24-bit images. It can read and write various image formats, draw primitive shapes like lines, and polygons, blend multiple images together in various ways, scale, crop, render text, etc..

To try the Imager module, you can create a new image and save to it to a file.

1. Make sure the Imager module is installed. You can use CPAN Shell to do it.

2. Write a Perl script to create an image of 200x200 px size and save it to Imager-New.bmp.

#- Copyright (c) All Rights Reserved.

use Imager;
$img = Imager->new(xsize=>200, ysize=>200);
print "Image created:\n";
print "   Width:  ", $img->getwidth(), "\n";
print "   Height: ", $img->getheight(), "\n";

$file = "Imager-New.bmp";
$img->write(file=>$file) or
      die $img->errstr;
print "Image saved:\n";
print "   File: $file\n";

3. Run the above script:

herong$ perl

Image created:
   Width:  200
   Height: 200
Image saved:
   File: Imager-New.bmp

herong$ ls -l
-rw-r--r--@ 1 herong  staff     396
-rw-r--r--@ 1 herong  staff  120054 Imager-New.bmp

4. View Imager-New.bmp. You see an image with black color.

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