1. Products
  2.   Image
  3.   Ruby
  4.   ChunkyPNG
 
  

Free Ruby Image Processing API for Reading & Writing Images

Open Source Ruby Image Processing Library that enables Software Developers to Create, Read, Edit, Manipulate, Re-size, Crop, Rotate and Convert PNG inside Ruby Applications.

What is ChunkyPNG?

ChunkyPNG is a powerful, open-source Ruby library designed specifically for comprehensive PNG image processing. As a pure Ruby solution with no external dependencies, it provides developers with a streamlined, elegant toolkit for everything from basic PNG image creation to sophisticated manipulation and transformation of PNG files. Its intuitive API simplifies complex tasks such as pixel-level manipulation, image compositing, and adding text to your image with full control over fonts and sizes. Whether you're building web applications, generating reports, or creating custom graphics, ChunkyPNG stands out as an efficient and versatile Ruby library for image processing that integrates seamlessly into any project.

Beyond core manipulation, ChunkyPNG excels in making advanced workflows accessible. Developers can effortlessly load and edit images, apply custom filters and effects, and perform essential format conversion between PNG and other popular formats like BMP, GIF, and JPEG. This versatility makes it an indispensable tool for modern software development, where the need to manipulate and render images is ever-present. Combining a clean, expressive syntax with robust functionality—from creating images from scratch to editing existing graphics—this library empowers Ruby developers to handle visual content with precision and ease. For any project requiring reliable, dependency-free image processing in Ruby, ChunkyPNG proves to be a remarkably capable and valuable asset in your development toolkit.

Previous Next

Getting Started with ChunkyPNG

The recommend way to install ChunkyPNG is using RubyGems. Please use the following command for a smooth installation.

Install ChunkyPNG via GitHub

gem install chunky_png
You can also download it directly from GitHub.

Creating PNG Images via Ruby API

One of the fundamental tasks you can perform with ChunkyPNG is creating a PNG image. The open source library makes it easy for Ruby developers to create a new PNG image from the scratch inside Ruby applications. The library supports several important features for handling images creation, such setting pixel colors, applying transparency, setting dimensions and many more. The following example demonstrates how software developers can create a 100x100 pixel red square, iterate through the coordinates and set the color at each pixel to red.

How to Create a New Images via Ruby API?

require 'chunky_png'
# Create a new image with a white background
image = ChunkyPNG::Image.new(100, 100, ChunkyPNG::Color::WHITE)
# Draw a red square on the image
(10..90).each do |x|
  (10..90).each do |y|
    image[x, y] = ChunkyPNG::Color.rgb(255, 0, 0)
  end
end
# Save the image to a file
image.save('red_square.png')

Load and Manipulate Images via Ruby

The open source library ChunkyPNG enables software developers to load and manipulate PNG images inside their Ruby applications. The library provides functions to load existing PNG images into memory, so you can modify and manipulate them as needed. The library excels in pixel-level operations, enabling users to change the color, transparency, and other properties of individual pixels within an image. It offers the ability to draw various shapes like lines, rectangles, and circles directly onto the image. The following simple example shows how to load an existing image and update its properties.

How to Load and Manipulate Images via Ruby API?

# Load an existing image
image = ChunkyPNG::Image.from_file('input.png')
# Invert the colors of the image
image.invert!
# Save the modified image
image.save('inverted.png')

Rendering Text & Format Conversion via Ruby

The open source ChunkyPNG library can be used for rendering text on images programmatically, which can be useful for creating captions, watermarks, or labels. Moreover, the library facilitates software developers in format conversion between PNG and other image formats like BMP, GIF, and JPEG. Here's an example of rendering text on an image that shows how users can create an image with a white background, set the font size and text color, and use the text method to render text onto the image.

How to Render Text on an Image using Ruby API?

require 'chunky_png'
# Create a new image with a white background
image = ChunkyPNG::Image.new(300, 100, ChunkyPNG::Color::WHITE)
# Set the font size and color
font_size = 20
text_color = ChunkyPNG::Color.rgb(0, 0, 0)
# Render text on the image
image.compose! do |canvas|
  canvas.text(10, 40, 'Hello, ChunkyPNG!', font_size, text_color)
end
# Save the image with the rendered text
image.save('text_image.png')

FAQ

Q1: What is ChunkyPNG and what does it do?

A: ChunkyPNG is a pure Ruby library for reading, writing, and manipulating PNG images without external dependencies.

Q2: Is ChunkyPNG free to use in my project?

A: Yes, ChunkyPNG is open-source and released under the MIT license, allowing free use in both personal and commercial projects.

Q3: Does ChunkyPNG require external libraries or system dependencies?

A: No, ChunkyPNG is written entirely in Ruby, so no external libraries or system installations are required.

Q4: Can ChunkyPNG create PNG images from scratch?

A: : Yes, you can create new PNG images programmatically by setting pixel colors and dimensions directly in Ruby.

Q5: Does ChunkyPNG support reading and editing existing PNG files?

A: Yes, it fully supports loading existing PNGs for editing, analyzing, or transforming their content.

 English