Ethan Hayon
Ethan Hayon

Reputation: 346

Alternatives to RMagick/ImageMagick for reading image pixel by pixel

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?

Upvotes: 4

Views: 828

Answers (1)

siame
siame

Reputation: 8657

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

Upvotes: 3

Related Questions