Python拾遗系列:从svg中提取数据

1. 下方的图片原数据是svg格式

目标: 从svg中提取划粗线的数据

搜狗截图20200426201212.png

2. 安装svg.path包

通过网络查询,找到了svg.path包

svg.path 是实现 SVG 中不同路径命令的对象的集合,也是 SVG 路径定义的解析器。

!pip install svg.path
Collecting svg.path
  Downloading svg.path-4.0.2-py2.py3-none-any.whl (21 kB)
Installing collected packages: svg.path
Successfully installed svg.path-4.0.2

2. 导入svg.path,xml等相关包

svg 本身也是一种xml格式文件

import xml.etree.ElementTree as etree
from svg.path import parse_path
import numpy as np
import matplotlib.pyplot  as plt

3. 解析Svg,找到曲线对应的path

wind = etree.parse('wind.svg')
g_nodes = wind.findall("{http://www.w3.org/2000/svg}g")
for g_node in g_nodes:
    print(g_node.attrib)
{'transform': 'scale(0.1)'}
{}
{'transform': 'scale(0.1)'}
{'stroke-width': '1', 'stroke': '#000', 'pointer-events': 'none', 'class': 'pointer_events_none', 'fill': 'none', 'stroke-opacity': '0.072'}
{'stroke': 'none', 'fill': '#000'}
{'pointer-events': 'none'}
children = g_nodes[0].findall("{http://www.w3.org/2000/svg}g")[0].getchildren()
child = children[0]

4. 用svg.path解析路径

ps = parse_path(child.attrib['d'])

5. 保存数据到xy数组

xy = []
for p in ps:
    if p.length() == 0:
        continue
    xy.append([p.start.real, p.start.imag])
    
xy = np.array(xy)
x, y = xy[:,0], xy[:,1]
y_new = y.max() + 300 - y
plt.figure(figsize=(8,1))
plt.plot(np.arange(x.shape[0]), y_new)
曲线
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容