Ben Bullock


Image::PNG - Read and write PNG files


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


Image::PNG is a layer on top of Image::PNG::Libpng. Whereas Image::PNG::Libpng copies the interface of the C library libpng, Image::PNG is intended to be a more intuitive way to handle PNG images.

Please note that this module is not completed yet and is still under development, so the interface may change. It's also open to suggestions for improvements.

General methods


    my $png = Image::PNG->new ();

Create a new PNG-file reading or writing object.

Options are

    my $png = Image::PNG->new ({read => 'some.png'});

Set the file to read. The file is then read at the time of object creation.

    my $png = Image::PNG->new ({verbosity => 1});

If verbosity is set to a true value, print verbose messages about what the module is doing.


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

Read the PNG from the file name specified as the argument. This dies if there is an error.


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

Write the PNG to the file name specified as the argument. This dies if there is an error.


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

Get the PNG image data as a Perl scalar.


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).


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

Get the width of the current PNG image.


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

Get the height of the current PNG image.


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

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



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

Get the bit depth of the current PNG image.


    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


    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.


    my $rows = $png->rows;

This method returns the rows of the image as an array reference, $rows. The array reference is a size 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


    my @text = $png->text;

Get the text chunks of the image. Each element of the return value is a hash reference. The keys are the fields of the PNG text chunk, and the values are the values of those fields in the text chunk. The size of the array is equal to the number of text chunks.


    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 the following six fields,


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.

If there is no last modification time, the undefined value is returned instead of a hash reference.


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


     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 a convenience function for debugging rather than a general-purpose routine.


There is a mailing list for this Perl module at Google Groups. If you have an enquiry, post to the mailing list. You don't have to join the mailing list to post a message.


In this distribution


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


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

To download libpng, see See also "Alien::PNG".

Other Perl modules on CPAN


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 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 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 reduces the size of dynamically created PNG images. It's very, very slow at reading PNG data, but seems to work OK.


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.


If you only need to find the size of an image, Image::Size can give you the size of PNG and various other image formats. This module is highly recommended on CPAN ratings.


Image::PNGwriter is an interface to a project called "PNGwriter". At the time of writing (2013-12-01), only one version has been released, in 2005. The most recent version of PNGwriter itself is from 2009.

About the PNG format

Wikipedia article

There is an article on the format on Wikipedia.

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


There is a collection of demonstrations and example scripts online at This currently contains

PNG inspector

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

PNG quantizer

This demonstration 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 using WWW::Imgur.


Ben Bullock, <>


The Image::PNG package and associated files are copyright (C) 2014 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.


Mailing list

There is a mailing list at It is not necessary to be a member of the list to post messages to the list or participate in discussions.

CPAN stuff

There is a bug tracker at


The distributed files are not the source code of the module. The source code lives in the "tmpl" directory of the distribution and the distribution is created via scripts.