Skip to content
Go to file
This branch is 3 commits ahead, 317 commits behind wvanbergen:master.

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Chunky PNG

This library can read and write PNG files. It is written in pure Ruby for maximum portability. Let me rephrase: it does NOT require RMagick or any other memory leaking image library.

Source code



Issue tracker


  • Decodes any image that the PNG standard allows. This includes all standard color modes, all bit depths and all transparency, interlacing and filtering options.

  • Encodes images supports all color modes (true color, grayscale and indexed) and transparency for all these color modes. The best color mode will be chosen automatically, based on the amount of used colors.

  • R/W access to the image's pixels.

  • R/W access to all image metadata that is stored in chunks.

  • Memory efficient (uses a Fixnum, i.e. 4 or 8 bytes of memory per pixel, depending on the hardware)

  • Reasonably fast for Ruby standards, by only using integer math and a highly optimized saving routine.

  • Interoperability with RMagick if you really have to.

Also, have a look at OilyPNG at OilyPNG is a mixin module that implements some of the ChunkyPNG algorithms in C, which provides a massive speed boost to encoding and decoding.


require 'chunky_png'

# Creating an image from scratch, save as an interlaced PNG
png =, 16, ChunkyPNG::Color::TRANSPARENT)
png[1,1] = ChunkyPNG::Color.rgba(10, 20, 30, 128)
png[2,1] = ChunkyPNG::Color('black @ 0.5')'filename.png', :interlace => true)

# Compose images using alpha blending.
avatar = ChunkyPNG::Image.from_file('avatar.png')
badge  = ChunkyPNG::Image.from_file('no_ie_badge.png')
avatar.compose!(badge, 10, 10)'composited.png', :fast_rgba) # Force the fast saving routine.

# Accessing metadata
image = ChunkyPNG::Image.from_file('with_metadata.png')
puts image.metadata['Title']
image.metadata['Author'] = 'Willem van Bergen''with_metadata.png') # Overwrite file

# Low level access to PNG chunks
png_stream = ChunkyPNG::Datastream.from_file('filename.png')
png_stream.each_chunk { |chunk| p chunk.type }

For more information, see the project wiki on or the RDOC documentation on


The library is written by Willem van Bergen for, and released under the MIT license (see LICENSE). Please contact me for questions or remarks. Patches are greatly appreciated!

P.S.: The name of this library is intentionally similar to Chunky Bacon and Chunky GIF. Use Google if you want to know _why. :-)


Pure ruby library for read/write access to PNG images.




You can’t perform that action at this time.