NAME

Image::PNG - Read and write PNG files

SYNOPSIS

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

VERSION

This documents version 0.25 of Image::PNG corresponding to git commit 33bd09704e977a7a2c48a728024c3e2c48c3defe made on Sat Mar 20 15:39:43 2021 +0900.

DESCRIPTION

This module is a simple interface for dealing with images in the PNG format. Image::PNG uses Image::PNG::Libpng to access PNG images.

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");

Read a PNG image 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.

PNG header-related methods

These methods set and get PNG header data (the IHDR chunk of the PNG file).

bit_depth

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

Get the bit depth 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

height

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

Get the height of the current PNG image in pixels.

interlacing_method

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

Get the name of the method of interlacing of the current PNG image. This may be either none or adam7.

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

width

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

Get the width of the current PNG image in pixels. For the number of bytes in a row, see the "rowbytes" method.

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. For the number of pixels in a row, see the "width" method.

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 bytes with the value zero.

If no image data has been read in, it prints a warning and returns the undefined value.

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 (UTC) time zone, so there is no time zone information.

If there is no last modification time, the undefined value is returned.

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

About the PNG format

Libpng.org website

http://www.libpng.org/ is the website for PNG and for the libpng implementation. To download libpng, see http://www.libpng.org/pub/png/libpng.html. See also "Alien::PNG".

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

DEPENDENCIES

Image::PNG::Libpng

SEE ALSO

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.

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.

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.

Image::PNG::Write::BW

Image::PNG::Write::BW is a pure-Perl module to write minimal black and white PNG images.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

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