如何使用Python的svgwrite模块应用alpha掩码?

时间:2015-03-10 16:36:41

标签: python svg

我正在使用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 很容易 Example of desired effect

2 个答案:

答案 0 :(得分:3)

谢谢Jonline! ClipPath和Mask的svgwrite“示例”没有实际显示如何将其应用于绘图。为了完整起见,我用你的方法为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))

Result of this code

答案 1 :(得分:1)

我怀疑这是一个不优雅或至少非常冗长的解决方案,但是,花了一个小时RTFM并玩弄后,我提出的一般解决方案是:

1)在SVG defs中定义掩码 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()