Convert SVG to PNG with Python on Windows

亡梦爱人 提交于 2021-02-18 16:58:08

问题


Question: Which reproducible process can enable Windows Python users to render a SVG image into PNG?


Many questions/answers (such as Convert SVG to PNG in Python and Server-side SVG to PNG (or some other image format) in python, which are not duplicates for the reasons explained below) explain how to convert a SVG to PNG with Python.

Unfortunately, none of them are ready-to-use for Python + Windows. After more than 20 minutes, and many different attempts, I'm still unable to do it. More details about failing attempts:

  • Installing cairo on Windows is not straightforward, we have to use Gohlke's binaries Intalling pycairo with Python 3.7 on Windows :

    pip install pycairo-1.20.0-cp37-cp37m-win_amd64.whl
    
  • Even once cairo is installed, rsvg (from main answers of Server-side SVG to PNG (or some other image format) in python, Convert SVG to PNG in Python) is not available for Windows:

    pip install rsvg   # or pyrsvg
    > ERROR: No matching distribution found for pyrsvg
    
  • Solutions with svglib or reportlab don't work out-of-the-box on Python3 + Windows:

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

    Indeed:

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

So a solution - specific for Windows - would be helpful.


回答1:


From the comments, the solution was to install svglib version 1.0.1 and reportlab 3.5.59.



来源:https://stackoverflow.com/questions/65759849/convert-svg-to-png-with-python-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!