NAME

Image::PNG - Read and write PNG (Portable Network Graphics) files

WARNING

This version of the module is solely for evaluation and testing. This module is currently incomplete and untested, and contains errors, bugs, and inconsistencies, including unresolved memory corruption errors which can crash Perl. The documentation below refers to functions which may or may not exist in the completed module and contains links to things which do not exist and may never exist.

SYNOPSIS

my $png = Image::PNG->new ();
$png->read_file ("crazy.png");
printf "Your PNG is %d x %d\n", $png->width, $png->height;

General methods

read_file

$png->read_file ("crazy.png")
    or die "Can't read it: " . $png->error ();

write_file

$png->write_file ("crazy.png")
    or die "Can't write it: " . $png->error ();

data

my $data = $png->data ();

Get the PNG binary data.

error

Print the most recent error message.

PNG header-related methods

These methods are related to the PNG header (the IHDR chunk of the PNG file).

width

my $height = $png->width ();

Get the width of the current PNG image.

height

my $height = $png->height ();

Get the height of the current PNG image.

color_type

my $color_type = $png->color_type ();

Get the name of the colour type of the current PNG image. The possible return values are

PALETTE
GRAY
GRAY_ALPHA
RGB
RGB_ALPHA

bit_depth

my $bit_depth = $png->bit_depth ();

Get the bit depth of the current PNG image.

interlacing_method

my $interlacing_method = $png->interlacing_method

Get the name of the method of interlacing of the current PNG image.

There is no method for dealing with the compression method field of the header, since this only has one possible value.

Image data-related methods

rowbytes

my $rowbytes = $png->rowbytes;

This method returns the number of bytes in each row of the image. If no image has been read yet, it returns the undefined value.

rows

my $rows = $png->rows;

This method returns the rows of the image as an array reference which contains a number of elements equal to the height of the image. Each element has the length of the number of bytes in one row (as given by rowbytes) plus one final zero byte. The row data returned is binary data and may contain several bytes with the value zero.

Non-image chunks

text

my @text = $png->text;

Get the text chunks of the image. Each chunk is a hash reference with the keys being the fields of the PNG text chunk and the values being the values of those fields.

time

my $time_ref = $png->time;
print "The PNG was last modified in $time_ref->{year}.\n";

Get the last modified time of the image. The return value is a hash reference containing six fields,

year
month
day
hour
minute
second

These represent the last modification time of the image. The modification time of a PNG file is meant to be in the GMT (UCT) time zone so there is no time zone information in this.

If there is no last modification time, a hash reference is returned but it doesn't contain any fields.

FUNCTIONS

There are some convenience functions in this module, exported on request.

display_text

use Image::PNG qw/display_text/;
my @text = $png->text;
display_text ($text[3]);

Display the text chunk given as an argument on STDOUT.

This is meant as a minimal convenience function for when you are debugging or something rather than a general-purpose text chunk display routine.

SUPPORT

There is a mailing list for this Perl module at Google Groups. If you have a question or suggestion or bug report, please let me know via the mailing list. You don't have to join the mailing list to post a message.

SEE ALSO

In this distribution

Image::PNG::Const

Image::PNG::Const contains the libpng constants taken from the libpng header file "png.h".

Image::PNG::Libpng

Image::PNG::Libpng provides a Perl mirror of the interface of the C PNG library "libpng". Image::PNG is built on top of this module.

libpng download

If you need to download libpng, see http://www.libpng.org/pub/png/libpng.html. See also "Alien::PNG".

Other Perl modules on CPAN

Image::ExifTool

Image::ExifTool is a pure Perl (doesn't require a C compiler) solution for accessing the text segments of images. It has extensive support for PNG text segments.

Alien::PNG

Alien::PNG claims to be a way of "building, finding and using PNG binaries". It may help in installing libpng. I didn't use it as a dependency for this module because it seems not to work in batch mode, but stop and prompt the user. I'm interested in hearing feedback from users whether this works or not on various platforms.

Image::PNG::Rewriter

Image::PNG::Rewriter is a utility for unpacking and recompressing the IDAT (image data) part of a PNG image. The main purpose seems to be to recompress the image data with the module author's other module Compress::Deflate7. Unfortunately that only works with Perl versions 5.12.

Image::Pngslimmer

Image::Pngslimmer reduces the size of dynamically created PNG images. It's very, very slow at reading PNG data, but seems to work OK.

Image::Info

Image::Info is a module for getting information out of various types of images. It has good support for PNG and is written in pure Perl (doesn't require a C compiler). As well as basics such as height, width, and colour type, it can get text chunks, modification time, palette, gamma (gAMA chunk), resolution (pHYs chunk), and significant bits (sBIT chunk). At the time of writing (version 1.31) it doesn't support other chunks.

About the PNG format

The PNG specification

The PNG specification (link to W3 consortium) explains the details of the PNG format.

PNG The Definitive Guide by Greg Roelofs

The book "PNG - The Definitive Guide" by Greg Roelofs, published in 1999 by O'Reilly is available online at http://www.faqs.org/docs/png/. I didn't refer to this book at all in making Image::PNG, so I can't vouch for it, but looking at the contents pages it appears to contain a lot of useful information, although it is definitely showing its age, with chapters about software such as Netscape Navigator and BeOS.

EXAMPLES

There is a collection of example scripts online at http://www.lemoda.net/image-png/. This currently contains

PNG inspector http://www.lemoda.net/png/inspect/

This downloads a PNG file you specify from the internet and prints out its contents.

PNG quantizer http://www.lemoda.net/png/quantize/

This downloads a PNG file you specify from the internet and quantizes it with as many colours as you want (from two to 256) in order to reduce its size. The quantized image is uploaded to imgur.com using WWW::Imgur.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

The Image::PNG package and associated files are copyright (C) 2011 Ben Bullock.

You can use, copy, modify and redistribute Image::PNG and associated files under the Perl Artistic Licence or the GNU General Public Licence.

SUPPORT

Mailing list

There is a mailing list at http://groups.google.com/group/perlimagepng. You don't need to be a member of the list to post messages to the list or participate in discussions. Your messages may be held for moderation though.

If you have anything at all to say about the module, whether it is bug reports, feature requests, or anything else, I'd prefer to discuss it on the mailing list because that way there is a public record which everyone can access, rather than being restricted to email. That means that, for example, if someone else takes over maintaining this module, they can easily access records of previous discussions.

CPAN stuff

There is a bug tracker at .

FOR PROGRAMMERS

(This section is only for people who want to fix a bug or add an improvement to this module, and who want to share the change with other people by adding it to the public version of this module.)

If you want to alter this module, note very carefully that the distributed files are not actually the source code of the module. The source code lives in the "tmpl" directory of the distribution and the distribution is created via scripts.

The original files of this distribution make very heavy use of the Template Perl module in order to cut down the amount of repetitive stuff which needs to be put into various source and documentation files. If you plan to alter the C file, you may also need a program called "cfunctions" which you can download from sourceforge, which generates the header file perl-libpng.h from perl-libpng.c.