Référence rapide

Aide-mémoire de la bibliothèque d'images ChunkyPNG pour Ruby

Guide de référence rapide pour ChunkyPNG, la bibliothèque PNG en pur Ruby : chargement, enregistrement, manipulation de pixels et découpe.

· Publié le

ChunkyPNG est une bibliothèque en pur Ruby pour lire, écrire et manipuler des images PNG sans nécessiter d'extensions C externes ni ImageMagick.

Chargement d'images (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)

Enregistrement et exportation d'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) }

Dessin de pixels et de formes géométriques

# 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)

Transformations et recadrage (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

Sujets associés