10

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.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • You cannot encode vector graphics data in a format that is raster based without loss of information. You're going to have to answer this question first: How much information are you willing to sacrifice? – IInspectable Jan 17 '21 at 12:07
  • @IInspectable I'm looking for a PNG export, for a given image size, e.g. `width=1000px` (Of course a SVG can be infinitely zoomed without loss of quality, which will not be the case for a PNG, but I'm ok with this). – Basj Jan 17 '21 at 12:13
  • That `svglib` example worked for me on Windows, I couldn't reproduce your error. Is the problem specific to certain SVG files you have? – Luke Woodward Jan 17 '21 at 12:24
  • @LukeWoodward Which versions of Python, svglib, reportlab do you have? I'll try to pip install the same versions. – Basj Jan 17 '21 at 12:26
  • I have Python 3.7.3 installed, and the latest svglib and reportlab (1.0.1 and 3.5.59), which I recently installed from `pip` without specifying a version number for either. Also, could you please edit your question to include the full traceback of that `AttributeError` you were receiving? – Luke Woodward Jan 17 '21 at 12:33
  • I reinstalled with the same versions than you, and it works indeed. I think you can post this as an answer! – Basj Jan 17 '21 at 12:35
  • Did you reinstall Python as well, or just svglib and reportlab? – Luke Woodward Jan 17 '21 at 12:39
  • Just the two last ones @LukeWoodward. – Basj Jan 17 '21 at 12:41
  • You should include updated answers to either or both of the questions you linked, now that you have a solution. – Peter O. Jan 30 '21 at 10:13

1 Answers1

9

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

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • This answer is life saver. I had `reportlab` 3.5.49 and still i was getting Attributerror. But with the required update its working fine. – 7bStan May 16 '21 at 05:24
  • Thanks a lot, you really saved my life. Great job! –  Mar 25 '22 at 19:15