I'm trying to render a SVG, it's content is pretty simple:
我正在尝试呈现一个SVG,其内容非常简单:
<svg width="3000" height="3000" xmlns="http://www.w3.org/2000/svg">
<path d="M217 279H473Q441 364 409.5 443.0Q378 522 345 607ZM-2 4V66H63L325 747H415L677 66H750V4H474V66H553L495 217H194L137 66H215V4Z" fill="red" />
</svg>
It's an alphabet (A), flipped vertically, that's all. Now I'm read this file using xml.etree.ElementTree
. Then parsing path with svg.path.parse_path
.
这是一个字母(A),垂直翻转,仅此而已。现在,我使用xml.etree.ElementTree读取该文件。然后使用svg.path.parse_path解析路径。
I've only one path here: M217 279H473Q441 364 409.5 443.0Q378 522 345 607ZM-2 4V66H63L325 747H415L677 66H750V4H474V66H553L495 217H194L137 66H215V4Z
. If I translate it into 2D coordinate system, it looks something like this:
我这里只有一条路径:M217 279H473Q441 364 409.5 443.0Q378 522 345 607ZM-2 4V66H63L325 747H415L677 66H750V4H474V66H553L495 217H194L137 66H215V4Z。如果我将其转换到2D坐标系,它看起来如下所示:
Path(Move(to=(217+279j)), Line(start=(217+279j), end=(473+279j)), QuadraticBezier(start=(473+279j), control=(441+364j), end=(409.5+443j), smooth=False), QuadraticBezier(start=(409.5+443j), control=(378+522j), end=(345+607j), smooth=False), Close(start=(345+607j), end=(217+279j)), Move(to=(-2+4j)), Line(start=(-2+4j), end=(-2+66j)), Line(start=(-2+66j), end=(63+66j)), Line(start=(63+66j), end=(325+747j)), Line(start=(325+747j), end=(415+747j)), Line(start=(415+747j), end=(677+66j)), Line(start=(677+66j), end=(750+66j)), Line(start=(750+66j), end=(750+4j)), Line(start=(750+4j), end=(474+4j)), Line(start=(474+4j), end=(474+66j)), Line(start=(474+66j), end=(553+66j)), Line(start=(553+66j), end=(495+217j)), Line(start=(495+217j), end=(194+217j)), Line(start=(194+217j), end=(137+66j)), Line(start=(137+66j), end=(215+66j)), Line(start=(215+66j), end=(215+4j)), Close(start=(215+4j), end=(-2+4j)))
Then I try to parse all nodes and edges from this data:
然后,我尝试解析该数据中的所有节点和边:
node: (-2.0, 4.0)
node: (-2.0, 66.0)
node: (63.0, 66.0)
node: (137.0, 66.0)
node: (194.0, 217.0)
node: (215.0, 4.0)
node: (215.0, 66.0)
node: (217.0, 279.0)
node: (325.0, 747.0)
node: (345.0, 607.0)
node: (409.5, 443.0)
node: (415.0, 747.0)
node: (473.0, 279.0)
node: (474.0, 4.0)
node: (474.0, 66.0)
node: (495.0, 217.0)
node: (553.0, 66.0)
node: (677.0, 66.0)
node: (750.0, 4.0)
node: (750.0, 66.0)
edge: (0, 0)
edge: (0, 1)
edge: (1, 2)
edge: (2, 3)
edge: (3, 0)
edge: (4, 4)
edge: (4, 5)
edge: (5, 6)
edge: (6, 7)
edge: (7, 8)
edge: (8, 9)
edge: (9, 10)
edge: (10, 11)
edge: (11, 12)
edge: (12, 13)
edge: (13, 14)
edge: (14, 15)
edge: (15, 16)
edge: (16, 17)
edge: (17, 18)
edge: (18, 19)
edge: (19, 4)
Then I'm using networkx
and matplotlib
together to visualize it:
然后我结合使用networkx和matplotlib来可视化它:
import networkx as nx
import matplotlib.pyplot as plt
# Create a new graph
G = nx.Graph()
# Add nodes to the graph
for node in nodes:
G.add_node(node)
print("node:",node)
# Add edges to the graph
for edge in edges:
e1, e2 = edge
G.add_edge(*(nodes[e1],nodes[e2]))
print("edge:",edge)
# Create a plot
pos = {node: (node[0], node[1]) for node in nodes}
nx.draw(G, pos, node_color='lightblue', edge_color='gray', node_size=1000)
nx.draw(G) #, pos, with_labels=True, node_color='lightblue', edge_color='gray', node_size=1000)
# plt.title(cha)
plt.show()
But what is being represented to me is quite different than expected:
但对我来说,代表的是与预期截然不同的:
Actual image:
实际图像:
I've always used some graphics library to render these stuff. This is my first time processing SVG files directly for rendering. Am I missing any steps? How can I resolve this issue?
我一直使用一些图形库来渲染这些东西。这是我第一次直接处理SVG文件进行渲染。我有遗漏什么步骤吗?我如何解决此问题?
Although it looks like an 'A', but when I try with other alphabets, it's nowhere close to that alphabet.
虽然它看起来像一个‘A’,但当我尝试其他字母表时,它与那个字母表相去甚远。
更多回答
我是一名优秀的程序员,十分优秀!