gpt4 book ai didi

python - 如何获得 svg 路径的点数

转载 作者:行者123 更新时间:2023-12-04 14:03:16 25 4
gpt4 key购买 nike

我有一个 SVG 文件,例如,这个

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" />
</svg>
如何获取这些路径的点 (x, y) 列表?
我已经看到 that 答案,但它不是完整的,所以我要求完整的解决方案。
我想有一个选项来选择有多少点或控制点的密度。

最佳答案

您可以在此处更改点的比例、偏移和密度:

from svg.path import parse_path
from xml.dom import minidom


def get_point_at(path, distance, scale, offset):
pos = path.point(distance)
pos += offset
pos *= scale
return pos.real, pos.imag


def points_from_path(path, density, scale, offset):
step = int(path.length() * density)
last_step = step - 1

if last_step == 0:
yield get_point_at(path, 0, scale, offset)
return

for distance in range(step):
yield get_point_at(
path, distance / last_step, scale, offset)


def points_from_doc(doc, density=5, scale=1, offset=0):
offset = offset[0] + offset[1] * 1j
points = []
for element in doc.getElementsByTagName("path"):
for path in parse_path(element.getAttribute("d")):
points.extend(points_from_path(
path, density, scale, offset))

return points


string = """<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke="red"
d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z" />
</svg>"""

doc = minidom.parseString(string)
points = points_from_doc(doc, density=1, scale=5, offset=(0, 5))
doc.unlink()
您还可以将这些点可视化:
import pygame
from svg.path import parse_path
from xml.dom import minidom


... # other functions and string


def main():
screen = pygame.display.set_mode([500, 500])
screen.fill((255, 255, 255))

doc = minidom.parseString(string)
points = points_from_doc(doc, 0.05, 5, (0, 5))
doc.unlink()

for point in points:
pygame.draw.circle(screen, (0, 0, 255), point, 1)

pygame.display.flip()

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return


pygame.init()
main()
pygame.quit()
density == 0.05 :
enter image description here density == 0.1 :
enter image description here density == 0.5 :
enter image description here density == 1 :
enter image description here density == 5 :
enter image description here

关于python - 如何获得 svg 路径的点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69313876/

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