gpt4 book ai didi

python - SVG 文件。删除元素

转载 作者:行者123 更新时间:2023-11-30 22:16:25 24 4
gpt4 key购买 nike

我正在尝试删除 id 为“area_3”的元素。我用过类似的东西:

for node in tree.xpath('//ellipse'):
node.getparent().remove(node)

SVG 示例:

<svg width="600" height="600" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
<g>
<title>Layer 1</title>
<image id="svg_1" y="0" x="0"/>
<image stroke="null" xlink:href="tehplan.jpg" id="svg_5" height="587.777769" width="585.333339" y="0.578137" x="20.083334"/>
<ellipse ry="19" rx="18" id="area_2" cy="172" cx="189" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
<ellipse id="area_3" ry="19" rx="18" cy="161" cx="275" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
</g>
</svg>

最佳答案

试试这个:

from lxml import etree

tree = etree.parse(open("so.svg"))
to_remove = tree.xpath("/svg:svg/svg:g/svg:ellipse[@id=\"area_3\"]",
namespaces={"svg": "http://www.w3.org/2000/svg"})[0]
g = to_remove.getparent()
g.remove(to_remove)
with open("so.out.svg", "wb") as o:
o.write(etree.tostring(tree, pretty_print=True))

输出:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600">
<!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
<g>
<title>Layer 1</title>
<image id="svg_1" y="0" x="0"/>
<image stroke="null" xlink:href="tehplan.jpg" id="svg_5" height="587.777769" width="585.333339" y="0.578137" x="20.083334"/>
<ellipse ry="19" rx="18" id="area_2" cy="172" cx="189" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke="#000000" fill="#ffffff"/>
</g>
</svg>

关于python - SVG 文件。删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49959111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com