【问题标题】:How to insert image into a circle element of svg? (Python: svgwrite)如何将图像插入到 svg 的圆形元素中? (Python:svgwrite)
【发布时间】:2021-09-24 09:49:09
【问题描述】:

我知道如何在普通 SVG 文件上执行此任务,但我在 python 3.7 中使用svgwrite 包,但我不知道如何执行此操作。可悲的是website documentation 没有足够准确地涵盖这个主题......

这是我想出的代码,但它不起作用:

image.add(image.ellipse(center=(half_x, half_y), r=(340, 340), fill=line_color, cx=50, cy=50, rx=80, ry=60, stroke="rgb(255,255,255)", opacity=0.3, stroke_width="0.5", id="img-container"))
image.add(image.image(href="https://images.pexels.com/photos/799443/pexels-photo-799443.jpeg", insert=None, size=("100%","100%"), clip_path="img-container"))

我只想在我的 SVG 文件中有一个圆形图像。如果您知道任何更好的方法,请告诉我!

编辑:当我想使用clipPath 时,我得到了以下错误:

ValueError: Invalid children 'image' for svg-element <clipPath>.

【问题讨论】:

    标签: python svg svgwrite


    【解决方案1】:

    我使用mask 来完成这项任务。首先,添加一个 id 为 wrapper 的掩码元素,然后添加一个圆形元素作为掩码的子元素。最后的技巧是将 mask 属性添加到图像元素中,例如 url(#bg_wrapper)。

    代码解决方案:

    image = svgwrite.Drawing(output_file, size=(str(width)+'mm', str(height)+'mm'))
    
    # Custom image
    mask = image.defs.add(image.mask(id="bg_wrapper"))
    mask.add(image.circle(center=(half_x, half_y), r=335, fill=line_color, opacity=".4"))
    image.add(image.image(href="image.jpeg", size=("100%", "100%"), mask="url(#bg_wrapper)"))
    

    【讨论】:

      猜你喜欢
      • 2014-05-26
      • 2012-05-31
      • 2012-07-14
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      相关资源
      最近更新 更多