Alternatives to RMagick/ImageMagick for reading image pixel by pixel

萝らか妹 提交于 2021-01-27 12:20:43

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/11905978/alternatives-to-rmagick-imagemagick-for-reading-image-pixel-by-pixel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!