Dependency-Free Access to PNG Image Files

Read and write access to PNG image files using the LodePNG library. The package has no external dependencies.

Build Status Build status codecov

The loder package provides functions for easily reading from PNG image files and writing to them. It functions in a very similar way to Simon Urbanek's venerable png package, but unlike that package it does not require external libpng and zlib libraries to be installed. Instead, loder includes Lode Vandevenne's compact LodePNG library, which provides self-contained PNG read and write functionality.


The package may be installed from CRAN using the standard command


Alternatively, the latest development version can be installed directly from GitHub using the devtools package, viz.

## install.packages("devtools")

Reading and writing files

The readPng function is used to read PNG images. It returns an array of class loder with three dimensions representing rows, columns and channels. Greyscale images have one channel, grey/alpha images have two, red/green/blue (RGB) images have three, and red/green/blue/alpha images have four.

path <- system.file("extdata", "pngsuite", "basn6a08.png", package="loder")
image <- readPng(path)
# [1] 32 32  4

Metadata including background colour, aspect ratio and pixel size or image resolution are available through attributes, if they are stored in the file.

path <- system.file("extdata", "pngsuite", "cdfn2c08.png", package="loder")
# $dim
# [1] 32  8  3
# $class
# [1] "loder" "array"
# $range
# [1]   0 255
# $asp
# [1] 4

The display function from the mmand package respects these attributes, and can be used to view the image on an R graphics device.

## install.packages("mmand")

Metadata alone may be read using the inspectPng function, which also gives information about the compression level achieved:

# PNG file: [...]/loder/extdata/pngsuite/cdfn2c08.png
# - 32 x 8 pixels, RGB
# - 8 bpp (uncompressed data size 768 B; file size is 404 B)
# - Aspect ratio is 4 : 1

Image data can be written back to a file using the writePng function. The theoretical numerical range of the pixel intensities (i.e., the black and white points) can be set using the range attribute or an argument of the same name.

writePng(image, tempfile(), range=c(0,200))

This will rescale the image intensities, and clip any values outside the specified range to the appropriate extreme.


This file documents the significant user-visible changes in each release of the loder R package.

loder 0.2.0

  • The new inspectPng function can be used to obtain details of a PNG file without obtaining the pixel data.
  • There is now a print method for objects of class "loder".
  • The writePng function now supports writing interlaced files, and offers different compression levels.
  • Text chunks are now read from and written to PNG files.
  • The LodePNG library has been updated.

loder 0.1.2

  • The package should now compile against versions of R prior to 3.3.0.
  • Some low-level memory protection and undefined behaviour warnings have been resolved.

loder 0.1.1

  • File arguments to readPng and writePng are now passed through path.expand, so that paths containing a tilde (~) will be handled properly.

loder 0.1.0

  • First public release.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2.0 by Jon Clayden, 3 years ago

Report a bug at

Browse source code at

Authors: Jon Clayden [aut, cre] , Lode Vandevenne [aut]

Documentation:   PDF Manual  

BSD_3_clause + file LICENCE license

Suggests testthat, covr

Suggested by RNiftyReg, mmand, tractor.base.

See at CRAN