【问题标题】:Convert SVG to PNG with Python on Windows在 Windows 上使用 Python 将 SVG 转换为 PNG
【发布时间】:2021-04-21 21:13:58
【问题描述】:

问题:哪个可重现的过程可以让 Windows Python 用户将 SVG 图像渲染成 PNG?


许多问题/答案(例如 Convert SVG to PNG in PythonServer-side SVG to PNG (or some other image format) in python,由于下面解释的原因不重复)解释了如何使用 Python 将 SVG 转换为 PNG。

很遗憾,它们都不能立即用于 Python + Windows。经过 20 多分钟和许多不同的尝试,我仍然无法做到。 有关失败尝试的更多详细信息:

  • 在 Windows 上安装 cairo 并不简单,我们必须使用 Gohlke 的二进制文件 Intalling pycairo with Python 3.7 on Windows

    pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
    
  • 即使安装了cairorsvg(来自Server-side SVG to PNG (or some other image format) in pythonConvert SVG to PNG in Python 的主要答案)也不适用于 Windows:

    pip install rsvg   # or pyrsvg
    > ERROR: No matching distribution found for pyrsvg
    
  • svglibreportlab 的解决方案不能在 Python3 + Windows 上开箱即用:

    from svglib.svglib import svg2rlg
    from reportlab.graphics import renderPDF, renderPM
    drawing = svg2rlg("a.svg")
    renderPM.drawToFile(drawing, "file.png", fmt="PNG")
    

    确实:

    AttributeError: 'Image' object has no attribute 'fromstring'
    

因此,特定于 Windows 的解决方案会有所帮助。

【问题讨论】:

  • 您不能以基于光栅的格式对矢量图形数据进行编码,而不会丢失信息。您必须首先回答这个问题:您愿意牺牲多少信息?
  • @IInspectable 我正在寻找一个 PNG 导出,对于给定的图像大小,例如width=1000px(当然,SVG 可以无限缩放而不会损失质量,PNG 则不会,但我可以接受)。
  • svglib 示例在 Windows 上为我工作,我无法重现您的错误。问题是否特定于您拥有的某些 SVG 文件?
  • @LukeWoodward 你有哪些版本的 Python、svglib、reportlab?我会尝试 pip 安装相同的版本。
  • 我安装了 Python 3.7.3,以及我最近从 pip 安装的最新的 svglib 和 reportlab(1.0.1 和 3.5.59),但没有指定任何一个版本号。另外,您能否编辑您的问题以包含您收到的AttributeError 的完整回溯?

标签: python windows svg cairo


【解决方案1】:

从 cmets 中,解决方案是安装 svglib 版本 1.0.1 和 reportlab 3.5.59。

【讨论】:

  • 这个答案是救命稻草。我有reportlab 3.5.49,但我仍然得到 Attributerror。但是通过所需的更新,它可以正常工作。
猜你喜欢
  • 2015-10-04
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2019-10-17
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
相关资源
最近更新 更多