我正在尝试使用RMagick
或Chunky_PNG
例如将每个像素的值读取为十六进制(HTML 表示法) #5DBCD2
。目前我有以下哪种方法可以满足我的需求,但我找不到实际读取十六进制值的正确方法。不过我更喜欢用Chunky_PNG
,谢谢!
require 'chunky_png'
img = ChunkyPNG::Image.from_file("image.png")
height = img.dimension.height
width = img.dimension.width
height.times do |i|
width.times do |j|
p [ChunkyPNG::Color.r(img[j,i]), ChunkyPNG::Color.g(img[j,i]), ChunkyPNG::Color.b(img[j,i])]
end
end
或者
require 'RMagick'
include Magick
image = ImageList.new("image.png")
(0..image.columns).each do |x|
(0..image.rows).each do |y|
pixel = image.pixel_color(x, y)
p [pixel.red, pixel.green, pixel.blue]
end
end