Python Convert SVG to PNG with CairoSVG for Beginners – Python Tutorial

By | August 27, 2019

To convert a svg file to png, we can use svglib library, you can read this tutorial to know how to convert with svglib.

Best Practice to Python Convert SVG to PNG with SvgLib – Python Tutorial

In this tutorial, we introduce how to convert a svg file to png with cairosvg, CairoSVG is more powerful than svglib. You can learn how to convert by referring to our tutorial.

Install CairoSVG

pip install cairosvg

Import library

import cairosvg

Convert svg to png file

svg = 'home.svg'
png = 'home.png'
cairosvg.svg2png(url= svg, write_to= png)

However, you may find this error when converting.

OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2 / cairo.so.2

To fix this error, you can read this solution.

Fix Win 10 OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2 / cairo.so.2 Error – Python Tutorial

Leave a Reply