Imager - Draw Graphical Elements

This section provides a tutorial example on how to draw graphical elements using line(), box(), circle() functions of the Perl Imager module.



With the Imager module, you can add graphical elements to an image using drawing fuctions like: line(), box(), circle(), etc..

1. Write a Perl script to create an image with some graphical elements added and save it to Imager-Graphics.bmp:

#- Imager-Draw-Graphics.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.

use Imager;
$img = Imager->new(xsize=>400, ysize=>400);

print "Drawing a box...\n";
$img->box(xmin=>50,ymin=>50, xmax=>350,ymax=>350, color=>'#0000ff',
      filled=>1);

print "Drawing a circle...\n";
$img->circle(x=>200,y=>200,r=>100, color=>'#00ff00', filled=>1);

print "Drawing 2 lines...\n";
$img->line(x1=>0,y1=>0, x2=>400,y2=>400, color=>'#ff0000');
$img->line(x1=>400,y1=>0, x2=>0,y2=>400, color=>'#ff0000');

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

2. Run the above script:

herong$ perl Imager-Draw-Graphics.pl
Drawing a box...
Drawing a circle...
Drawing 2 lines...
Image saved:
   File: Imager-Graphics.bmp

3. View Imager-Graphics.bmp. You see a nice image.



 

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

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 References

 Full Version in PDF/EPUB