Python中得到.svg文件矢量路径的坐标 [英] Python get coordinates of the vector path from .svg file

查看:3094
本文介绍了Python中得到.svg文件矢量路径的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,对于一个学校项目荫应该所谓的V型绘图仪的计划,我的团队建造它,我设法所有的计算编程等等,basicly我可以把它移动到一个特定的X / Y坐标和电梯/抬高笔。我转换的图像的potrace一个.svg文件,并就我的理解是,该文件应包含该绘图仪将遵循一定的路径。

我的想法在这里是读矢量路径,并通过阵列多个向量保存,然后简单地使一个循环经历这些阵列,从而绘制矢量。
或者干脆说:
循环变量i = 0
循环:


  1. 提笔

  2. 将到阵列(X1 / Y1)I

  3. 下笔

  4. 将到阵列(X2 / Y2)I

  5. 增加1我
    (这不是程序code,但易于编程)

但我无法找到任何方式来读取向量坐标,它不应该很容易得到所在的坐标向量开始(X1 / Y1),并在那里停止(X2 / Y2)?


解决方案

SVGs是非常简单的格式,你可以简单地用记事本打开它,看到XML code吧。的XML标记是需要绘制曲线的类型,并且它可以有其内部的一些元数据。您可以直接使用Python,如果你想读这个XML。

但SVGs并不像你假定它是那么容易 - 它不是简单的一堆向量(不是经典的矢量反正)

在SVG,你基本上确定的形状 - 直线,圆弧,CubicBezier,等你需要每个这些落实到你的程序,因为有些东西,显示SVG图像预期已经了解这些。


现在,你的问题问了一下包,我补充说,对于SVG在python处理有用的包:

svg.path - https://pypi.python.org/pypi/svg.path - 是一个伟大的图书馆创建路径,也得到XML信息并转换成可用的类,如电弧等,但我不认为他们有支持读取完整的SVG文件。

pysvg - https://pypi.python.org/pypi/pysvg/0.2。 2 - 是另一个utilitiy我用了一段时间后,可以导入SVG,使班像折线 圈子 椭圆等。

So for a school project Iam supposed to program a so called "V-Plotter", my team built it and I managed to program all calculations and so on, basicly I can make it move to a certain x/y coordinate and lift/raise the pen. I converted an Image with potrace to an .svg file and as far as I understood it, the file should contain a certain path which the plotter would follow.

My idea here would be to read the vector path and save it in multiple vectors via an Array, then simply make an loop going through those arrays and thus drawing the vectors. Or simply said: Loop variable i=0 Loop:

  1. pen up
  2. move to (x1/y1) of Array i
  3. pen down
  4. move to (x2/y2) of Array i
  5. increase i by 1 (this is not program code, but easy to program)

But i can not find any way to read the vector coordinates, shouldn't it be easy to get the coordinates where a vector starts (x1/y1) and where it stops (x2/y2)?

解决方案

SVGs are very simple formats, you could simply open it with a notepad and see the xml code for it. The xml tag is the type of curve you need to plot, and it can have some metadata inside it. You can directly use python to read this XML if you want.

But SVGs aren't as easy as you're assuming it to be - it isn't simply a bunch of vectors (not the classical vectors anyway)

In SVG, you basically define shapes - like Line, Arc, CubicBezier, etc. You'd need to implement each of these into your program, because something that shows an SVG image is expected to understand these already.


Now, as your question asked about the packages, I'm adding packages that are useful for SVG handling in python:

svg.path - https://pypi.python.org/pypi/svg.path - is a great library to create paths and also to get the XML info and convert into usable classes like Line, Arc etc. But I dont think they have support to read complete svg files.

pysvg - https://pypi.python.org/pypi/pysvg/0.2.2 - is another utilitiy i used a while back which can import an svg and make classes like PolyLine circle ellipse etc.

这篇关于Python中得到.svg文件矢量路径的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆