python - 如何使用 Python 获取 SVG 路径的高度、宽度和长度?

标签 python svg

我有一个包含大量路径的 svg,基本上是这样的:

<path fill="#fb6430" opacity="1.00" d=" M 0.70 0.00 L 14.60 0.00 C 16.36 3.76 19.56 6.54 21.77 9.99 C 21.37 11.15 20.65 12.15 20.02 13.20 C 18.65 11.77 17.81 9.99 17.51 8.04 C 16.28 8.93 15.13 9.92 14.08 11.01 C 13.93 9.08 13.77 7.16 13.62 5.23 C 13.03 5.22 11.84 5.20 11.25 5.19 C 11.35 6.73 11.45 8.26 11.58 9.79 C 10.84 9.90 9.37 10.13 8.63 10.24 C 7.87 9.37 7.10 8.51 6.36 7.63 C 6.08 7.25 5.53 6.51 5.25 6.13 C 3.75 4.08 2.19 2.06 0.70 0.00 Z" />

我想从这条路径中获取高度、宽度、面积、长度以及我基本上可以获取的任何其他信息。有没有python库?还是我忽略了一些我可以手动完成的地方?

最佳答案

您可以使用 svgpathtools找到这样的测量值(假设我知道你的意思)。

# create svgpathtools Path objects from an SVG file
from svgpathtools import svg2paths
paths, attributes = svg2paths('some_svg_file.svg')

# let's take the first path as an example
mypath = paths[0]

 # Let's find its length
print("length = ", mypath.length())

# Find height, width
xmin, xmax, ymin, ymax = mypath.bbox()
print("width = ", xmax - xmin)
print("height = ", ymax - ymin)

# Let's find the area enclosed by the path (assuming the path is closed)
try:
    print("area = ", mypath.area())
except AssertionError:
    print("This path is not closed!")
    # if you want a notion of area for an open path, 
    # you could use the bounding box area.
    print("height*width = ", (ymax - ymin)*(xmax - xmin))

关于python - 如何使用 Python 获取 SVG 路径的高度、宽度和长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35260742/

相关文章:

python - 多处理超时?

python - Pandas:对于 groupby value_counts,返回具有最大计数的行

javascript - Yeoman/Grunt 和 Grunticon 工作流程

reactjs - 使用 typescript 在 ref 上创建 svg 节点?

css - Firefox 和响应式 SVG

javascript - SVG 缩放动画在 iOS 上速度缓慢

Python argparse 默认字符串值

python - 在 ubuntu 中安装 Google Cloud SDK 时遇到问题

python - 如何比较python中的两个时区?

svg - 三.js : SVGRenderer?