Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SVG to PNG with Python on Windows

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.

like image 901
Basj Avatar asked Jan 17 '21 11:01

Basj


People also ask

How do I convert SVG to PNG?

Navigate to an . svg file, right click on it and click on the context menu item 'Save SVG as PNG. Lets you click on the extension icon or right click on an . svg file and choose Save SVG as PNG.

How do I convert SVG files to Windows?

Go to File>Export>Export As. Choose SVG as the output format and change the file settings, continue with Export to start the conversion.

Does PIL support SVG?

Image format (can be jpeg, png, svg and many others)

How do I convert SVG to PNG for free?

SVG to PNG – Convert SVG files to PNG Online Use this free online SVG to PNG converter to convert SVG files to PNG images, quickly and easily, without having to install any software. Click the UPLOAD FILES button and select up to 20 SVG files you wish to convert.

Is it possible to get a svg2png file in Python?

cairosvg supports only Python 3.4+. They have dropped Python 2 support There wasn't a svg2png for me, I had to use cairosvg.surface.PNGSurface.convert (svg_str, write_to='output.png'). The answer is " pyrsvg " - a Python binding for librsvg. There is an Ubuntu python-rsvg package providing it.

How do I open and edit an SVG file?

It is possible to use Adobe programs for opening and editing SVG files. Just be sure to install the SVG Kit for Adobe Creative Suite plug-in first. Converting SVG files is possible with the aid of a few online tools. For conversion to non-vector file types, try our SVG to GIF or SVG to PDF tools.

How to convert JPG_to_PNG file to PNG file?

Step 1: Import the library. In Function jpg_to_png we first Check whether The Selecting the image is in the same Format ( .jpg) which to convert to . png if not then return Error. Else Convert the image the to .png messagebox.showerror ("Fail!!", "Something Went Wrong...")


Video Answer


1 Answers

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

like image 151
Luke Woodward Avatar answered Oct 23 '22 18:10

Luke Woodward