The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Image::PNG - Read and write PNG files

SYNOPSIS

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

VERSION

This documents version 0.23 of Image::PNG corresponding to git commit dff4678b1deaa602af5b7a155989cead1afbe026 made on Sun Dec 11 08:12:23 2016 +0900.

DESCRIPTION

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

new

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

Create a new PNG-file reading or writing object.

Options are

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

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

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

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

read

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

write

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

data

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

Get the PNG image data as a Perl scalar.

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, $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

text

    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.

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 the following 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.

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

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

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

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.

Image::Size

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

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 http://www.faqs.org/docs/png/.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

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

FOR PROGRAMMERS

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.