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.

Images play a significant role in modern web and application development. Whether it's a simple logo, a complex diagram, or an interactive graphic, the need to manipulate and render images is a common task in software development. Ruby, known for its elegant and expressive syntax, offers a myriad of libraries to simplify various programming tasks. ChunkyPNG, a powerful and versatile Ruby library for image processing, stands out as an excellent tool for developers looking to work with PNG images efficiently.

ChunkyPNG is an open-source, pure Ruby library that enables software developers to work with PNG (Portable Network Graphics) images effortlessly. It provides a wide range of features, from basic image creation to advanced manipulation and transformation of PNG files. Unlike some other image processing libraries, it does not rely on external dependencies, making it easy to integrate into Ruby projects. It supports features like PNG image creation, image loading, pixel-level manipulation, apply filters and effects, image compositing, add text to your image, specifying fonts & sizes, and many more.

ChunkyPNG is very easy to handle and facilitates software developers in format conversion between PNG and other image formats like BMP, GIF, and JPEG. It is a versatile and powerful Ruby library for working with PNG images. Whether you need to create images from scratch, manipulate existing ones, or add text to your graphics, the library provides the tools you need to handle PNG images effectively. Its simplicity and ease of use make it a great choice for any Ruby developer dealing with image manipulation. Give it a try, and you'll find it a 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')

 English