Skip to content
Advertisement

Convert SVG to PNG in Python

How do I convert an svg to png, in Python? I am storing the svg in an instance of StringIO. Should I use the pyCairo library? How do I write that code?

Advertisement

Answer

The answer is “pyrsvg” – a Python binding for librsvg.

There is an Ubuntu python-rsvg package providing it. Searching Google for its name is poor because its source code seems to be contained inside the “gnome-python-desktop” Gnome project GIT repository.

I made a minimalist “hello world” that renders SVG to a cairo surface and writes it to disk:

JavaScript

Update: as of 2014 the needed package for Fedora Linux distribution is: gnome-python2-rsvg. The above snippet listing still works as-is.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement