有没有人有一个如何使用svgwrite将SVG过滤器应用于SVG组的示例?
这是我想要做的事情:
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()
结果未按预期将过滤器传递到组。
>>> 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>'
非常感谢任何帮助!
答案 0 :(得分:3)
下面是在具有单个矩形的组上使用简单模糊滤镜的示例。我可以在https://docs.google.com/folder/d/0BwFQiTKfux0qY1Y2d1hRdndtSEk/edit
找到更多我的svgwrite示例#!/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')