如何使用 Python 的 svgwrite 模块应用 alpha 蒙版?

How to apply an alpha mask using Python's svgwrite module?

我正在使用 svgwrite 模块以编程方式为项目创建一些图像。我需要使用 alpha 蒙版来创建渐变透明元素。我不清楚如何实际 应用 蒙版创建后。

在下面的示例中,我尝试使用圆形渐变在原本为灰色的正方形中创建渐变透明环。我可以创造广场;我可以分别创建圆形渐变 def 和一个用它填充的圆。

把一个敷在另一个上当面膜我不明白。

def my_mask():
            #  define some params
            width = "500px"
            height = "500px"
            radius = "50%"
            black = "rgb(255, 255, 255)"
            grey = "rgb(127,127,127)"

            #  create the drawing surface
            canvas = svgwrite.Drawing('temp_mask.svg', (width, height))

            #  create defs, in this case, just a single gradient
            rad_grad = canvas.radialGradient(("50%", "50%"), "50%", ("50%", "50%"), id="rad_grad")
            rad_grad.add_stop_color("0%", black, 0)
            rad_grad.add_stop_color("66.6%", black, 255)
            rad_grad.add_stop_color("100%", black, 255)

            #  now to draw; first I create the rect object to be masked
            base_rect = canvas.rect( (0, 0), (width, height), id="masked_rect").fill(grey)
            canvas.add(base)
    
            #  and according to the docs, any SVG fragment can be an alpha mask, so I create this circle
            mask_element = canvas.circle((radius, radius), radius, fill="url(#rad_grad)")

            #  but here's where I get confused; I don't get how this function actually makes use of one element to mask another
            canvas.mask((0, 0), (width, height))

        #  No problem exporting to a file, though. :)
        canvas.save()

这就是我想要的(刚刚添加红叉以展示透明度);使用 Sketch 很容易做到

我怀疑这是一个不优雅或至少非常冗长的解决方案,但是,又花了 RTFM 一个小时并进行了尝试后,我想出的一般解决方案是:

1) 在SVG defs中定义mask 2) 将 SVG 对象添加到所述蒙版以创建 alpha 贴图 3)可以预见的是,在被遮罩对象的遮罩属性中引用遮罩:

注意:我的渐变值也有错误,因此它们永远无法生成我包含的图像;下面的代码也解决了这个问题。

def my_mask():
            #  define some params
            width = "500px"
            height = "500px"
            radius = "50%"
            white = "rgb(255, 255, 255)"
            black = "rgb(0, 0, 0)"
            grey = "rgb(127,127,127)"

            #  create the drawing surface
            canvas = svgwrite.Drawing('temp_mask.svg', (width, height))

            #  create defs, in this case, just a single gradient
            rad_grad = canvas.radialGradient(("50%", "50%"), "50%", ("50%", "50%"), id="rad_grad")
            rad_grad.add_stop_color("0%", black, 0)
            rad_grad.add_stop_color("66.6%", white, 255)
            rad_grad.add_stop_color("100%", white, 255)
            canvas.defs.add(rad_grad)

            #  create the mask container as a def and include alpha-mapping objects
            mask = canvas.mask((0, 0), (width, height), id="grad_mask")
            mask.add(canvas.rect( (0, 0), (width, height) ).fill(white)
            mask.add(canvas.circle((radius, radius), radius, fill="url(#rad_grad)")
            canvas.defs.add(mask)

            #  now to draw; create the rect object and simply include the mask as an attribute
            base_rect = canvas.rect( (0, 0), (width, height), mask="url(#grad_mask)".fill(grey)
            canvas.add(base)

            #  Still no problem exporting to a file, though. ;)
            canvas.save()

谢谢乔琳! ClipPath 和 Mask 的 svgwrite "example" 没有实际向您展示如何将其应用于绘图。为了完整起见,我用你的方法为 ClipPath 写了一个真实的例子:

clip_path = dwg.defs.add(dwg.clipPath(id='my_clip_path1')) #name the clip path
clip_path.add(dwg.circle((5*mm, 5*mm), 10*mm)) #things inside this shape will be drawn
testCircle = dwg.add(dwg.g(id='test', stroke='red', stroke_width=1, fill='black', fill_opacity=1, clip_path="url(#my_clip_path1)"))
testCircle.add(dwg.circle((5*mm, 10*mm), 10*mm))