我如何使用Ruby中的路径裁剪图像?

问题描述 投票:0回答:1

这里是路径“ M276,189h268c5.5,0,10,4.5,10,10v196c0,5.5-4.5,10-10,10H276 c-5.5,0-10-4.5-10-10V199C266,193.5,270.5, 189,276,189z“

这是输入图像:

Input image

使用下面的代码将路径应用于图像之后

draw = Magick::Draw.new
draw.fill 'red'
draw.path path
draw.clip_rule("evenodd")
draw.fill_rule("evenodd")
draw.fill_opacity(0)
draw.draw image
img.trim!
img.write('output.jpg')

这是输出图像:

Output image

现在,我想剪切图像的红色部分。这意味着期望棕色部分仅可见。我使用了普通的图像级裁剪方法。这样做只能提取红色部分但是我想从输出图像中提取红色以外的图像。

这里是黑色图像的样本输出sample output for the black color

也许我们必须反转剪辑或反转裁剪才能获得此效果...

ruby-on-rails ruby image-processing rmagick image-extraction
1个回答
0
投票

方法

img.paint_transparent

将使红色部分透明,但是我们必须将颜色作为参数传递给上述方法。最初,我尝试将颜色值设置为红色。所以不行现在正在从图像中读取一个像素,如

redPixel = img.get_pixels(300,200,1,1)[0]

并从类似像素的redPixel.to_color中获取颜色并将值传递给上述方法...

我们必须为图像设置模糊值

img = Magick :: Image.read(“ diecut.jpg”)。first

redPixel = img.get_pixels(300,200,1,1)[0]

img.fuzz = '25%'

放入redPixel.to_color

newimage = img.paint_transparent(redPixel.to_color)

newimage.write(“ outPut.png”)

newimage.display

© www.soinside.com 2019 - 2024. All rights reserved.