简体   繁体   English

如何使用svgwrite和Python将SVG过滤器应用于组?

[英]How to apply SVG Filter to Group using svgwrite with Python?

Does anybody have an example of how to apply SVG filter to a SVG group using svgwrite? 有人有使用svgwrite将SVG过滤器应用于SVG组的示例吗?

Here's what I'm trying to do: 这是我想做的事情:

import svgwrite

dwg = svgwrite.Drawing('test.svg', profile='full')
grp = dwg.g()
grp.add(dwg.rect(insert=(5,5),size=(20,20)))

filtr = dwg.defs.add( dwg.filter(id="Ga",filterUnits="userSpaceOnUse") )
feGauss = filtr.feGaussianBlur()

grp.filter = feGauss   # This does not work
dwg.add(grp)
dwg.save()

The result does not pass filter onto the group as expected. 结果未按预期将过滤器传递到组。

>>> dwg.tostring()
u'<svg baseProfile="full" height="100%" version="1.1" width="100%"     xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><defs /><g><rect height="20" width="20" x="5" y="5" /></g></svg>'

Any help is highly appreciated! 任何帮助深表感谢!

Below is an example of using a simple blur filter on a group with a single rectangle. 以下是在具有单个矩形的组上使用简单模糊滤镜的示例。 More of my svgwrite examples can be found at https://docs.google.com/folder/d/0BwFQiTKfux0qY1Y2d1hRdndtSEk/edit 我的更多svgwrite示例可以在https://docs.google.com/folder/d/0BwFQiTKfux0qY1Y2d1hRdndtSEk/edit中找到

#!/usr/bin/python3
# License: MIT
import svgwrite

progname = 'example_filter_on_group'

def create_svg(name):

    svg_size_w = 900
    svg_size_h = 1500
    title_size = 20
    y = 0
    title = name + ': example of filter on a group'
    dwg = svgwrite.Drawing(name, (svg_size_w, svg_size_h), debug=True)
    # background will be white.
    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='white'))

    # create simple filter to blur rectangle
    blur6_filter = dwg.defs.add(dwg.filter())
    blur6_filter.feGaussianBlur(in_='SourceGraphic', stdDeviation=6)

    # group with filter
    g_f = dwg.add(dwg.g(filter=blur6_filter.get_funciri()))
    g_f.add( dwg.rect(insert=(50, 50), size=(50, 50), fill='aqua'))

    dwg.save()

if __name__ == '__main__':
    create_svg(progname + '.svg')
  • Lawrence 劳伦斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM