Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to RMagick/ImageMagick for reading image pixel by pixel

Tags:

ruby

I am currently using RMagick and ImageMagick on a project I am working on, an ASCII image generator: https://github.com/ehayon/Pixie

However, I don't like the ImageMagick dependency. I'm having a hard time finding an alternate library. All I need to do is get the RGB value at each pixel of an image. I'd like to support PNG and JPEG at a minimum.

Anybody have experience with a similar library without the ImageMagick dependency?

like image 317
Ethan Hayon Avatar asked Oct 22 '25 14:10

Ethan Hayon


1 Answers

Still looking for one on JPEG, but there's quite a nice library for PNG called Chunky PNG, which allows you to traverse and read the pixels in the image. Here's a little example going row by row:

require 'rubygems'
require 'chunky_png'

image = ChunkyPNG::Image.from_file('image.png')

(0..image.dimension.width).each do |x|
  (0..image.dimension.height).each do |y|
    r = ChunkyPNG::Color.r(image[x,y])
    g = ChunkyPNG::Color.g(image[x,y])
    b = ChunkyPNG::Color.b(image[x,y])
  end
end
like image 165
siame Avatar answered Oct 25 '25 18:10

siame