This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

ChunkyPNG Ruby Image Library Cheat Sheet

A quick reference guide for ChunkyPNG, the pure Ruby PNG library: loading, saving, pixel manipulation, cropping, canvas drawing, and image transforms.

· Published

ChunkyPNG is a pure-Ruby library to read, write, and manipulate PNG images without requiring external C extensions or ImageMagick.

Loading Images

require 'chunky_png'

# Load directly from filesystem
image = ChunkyPNG::Image.from_file('input.png')

# Load from binary in-memory blob or IO stream
image = ChunkyPNG::Image.from_blob(File.binread('input.png'))
image = ChunkyPNG::Image.from_io(io_stream)

Saving Images

# Save to file
image.save('output.png')

# Export to binary string blob
binary_blob = image.to_blob

# Write to stream
File.open('stream.png', 'wb') { |io| image.write(io) }

Pixel Drawing & Colors

# Set individual pixel RGBA values
image[0, 0] = ChunkyPNG::Color.rgba(255, 0, 0, 128)

# Color constructors
red  = ChunkyPNG::Color.from_hex('#ff0000')
cyan = ChunkyPNG::Color.rgba(0, 255, 255, 255)

# Draw geometric primitives
image.line(10, 10, 100, 10, red)
image.rect(20, 20, 80, 80, red, cyan) # border, fill
image.circle(50, 50, 25, red)

Canvas Operations & Transforms

# Crop sub-rectangle: crop(x, y, width, height)
cropped = image.crop(10, 10, 200, 200)

# Flipping and rotation
transformed = image.flip_horizontally.rotate_right
flipped_v   = image.flip_vertically

Related topics