我想使用 Ruby-Vips 提供的方法创建一个给定大小的纯色背景画布。现在,我可以这样做,如下所示:
canvas = Vips::Image.black(600, 600).ifthenelse([0,0,0], [252, 186, 3])
但是,必须创建一个黑色图像,然后以这种方式逐个像素地应用颜色,这似乎很奇怪。有没有更好或更有效的方法来实现这一点?
如果您尝试匹配现有图像,可以使用new_from_image
:
y = x.new_from_image [1, 2, 3]
制作从 复制大多数属性的图像x
,但每个像素值都由 替换[1, 2, 3]
。
的源代码new_from_image
显示了如何从头开始有效地制作图像:
def new_from_image value
pixel = (Vips::Image.black(1, 1) + value).cast(format)
image = pixel.embed 0, 0, width, height, extend: :copy
image.copy interpretation: interpretation, xres: xres, yres: yres,
xoffset: xoffset, yoffset: yoffset
end
所以: