Ruby r检查每个像素,它是如何工作的?

Ruby r检查每个像素,它是如何工作的?,ruby,pixel,rmagick,Ruby,Pixel,Rmagick,我需要操纵rmagick中图像的每个像素。我在IRB(交互式ruby)中做这件事,这就是我所拥有的: require 'Rmagick' include Magick f = Image.new(100,100) f.display #so far so good. A 100x100 white image is displayed f.each_pixel {|pixel, c, r| pixel.red = 0} f.display #the image is still white.

我需要操纵rmagick中图像的每个像素。我在IRB(交互式ruby)中做这件事,这就是我所拥有的:

require 'Rmagick'
include Magick
f = Image.new(100,100)
f.display #so far so good. A 100x100 white image is displayed

f.each_pixel {|pixel, c, r| pixel.red = 0}
f.display #the image is still white. It should really be a shade of blue.

我做错了什么?

问题是,从每个像素返回的数组是一个新的数据集。数据需要存储回图像

使用“获取像素”和“存储像素”:

img = Magick::ImageList.new('img.jpg').first
pixels = img.get_pixels(0,0,img.columns,img.rows)

for pixel in pixels
    avg = (pixel.red + pixel.green + pixel.blue) / 3
    pixel.red = avg
    pixel.blue = avg
    pixel.green = avg
end

img.store_pixels(0,0, img.columns, img.rows, pixels)
img.display

哈忘记了这一点,谷歌搜索了同样的问题,发现了这个:)再次感谢布莱斯:)