有没有办法仅使用纯Python或Python模块(例如wand)将png文件转换为SVG文件?
更准确地说,我想将png转换为真正的矢量图形,我不想在svg内嵌入位图,我想转换为图形代码。
我知道这可以通过 Illustrator 或 Inkscape 实现,但我需要一个自动化流程。
谢谢你!
您需要运行外部程序来进行图像跟踪。一个流行的程序是 potrace。这是 Inkscape 用来执行任务的工具。
有一些Python 绑定:
由于
potrace
模块在 Windows 上的 anaconda 上不(容易)可用,所以我寻找替代方案。 PngToSvg 在 Windows 10 Pro N 上的 Anaconda 4.8.3 中的 python 3.6 环境中完美运行。
您可以克隆/下载存储库并运行
python init.py
,这会将 example.png
中的 /examples/
转换为 example.svg
文件。
感谢存储库的作者。
我建议对 python 使用 potrace。
使用此链接:https://pypi.org/project/pypotrace/
这是文档:https://pythonhosted.org/pypotrace/ref.html#
像这样:
from potrace import Bitmap
# Initialize data, for example convert a PIL image to a numpy array
# [...]
bitmap = Bitmap(data)
path = bitmap.trace()
我正在使用 https://github.com/ianmackinnon/png2svg 将低分辨率 QR 码转换为 SVG,以便以任意尺寸打印它们。 它工作得很好,代码看起来符合预期,并且可以扩展到任意大小。
但是,有 2 个小(但可以忽略不计,至少对我来说)警告:
- 在我的机器上转换低分辨率图像 (400x400) 大约需要一分钟左右
- 与优化的 SVG 相比,文件大小大于应有的大小
pip install aspose-words
Aspose.Words for Python 是一个文档处理库,允许开发人员处理文档
import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image("Input.png")
shape.image_data.save("Output.svg")