python - 在 Windows 上使用 Python 将 SVG 转换为 PNG

标签 python windows svg cairo

问题:哪个可重现的过程可以使 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 多分钟和许多不同的尝试,我仍然无法做到。
有关失败尝试的更多详细信息:

  • 安装 cairo在 Windows 上并不简单,我们必须使用 Gohlke 的二进制文件 Intalling pycairo with Python 3.7 on Windows :
    pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
    
  • 甚至一次 cairo已安装,rsvg (来自 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
    
  • svglib 的解决方案或 reportlab不要在 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 的解决方案会有所帮助。

    最佳答案

    根据评论,解决方案是安装 svglib版本 1.0.1 和 reportlab 3.5.59。

    关于python - 在 Windows 上使用 Python 将 SVG 转换为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65759849/

    相关文章:

    python - 多元线性回归: appending an array on ones to Matrix of features (Python)

    python - 车轮名称与平台无关,即使我的包包含已编译的库

    python - Python切片语法list[::-1]中第一个冒号怎么解释

    c++ - 如何确定 Windows 驱动程序(又名内核空间)的 CPU 和内存消耗

    asp.net - Docker 拉取显示 "Unknown blob"

    C# SVG(使用 device-cmyk() 或 icc-color())到 PDF 转换

    python - 如何读取和解码印度 Aadhaar 卡图像上的安全二维码

    c++ - LookupAccountSid() 中的错误 0x8007000e

    javascript - D3 渲染 SVG。我如何以编程方式获取该 SVG?

    javascript - 使用 SVG 生成圆形按钮