rsvg with Python 3.2 on Ubuntu
Asked Answered
L

1

8

I am trying to use rsvg in Python 3.2 but I keep getting an import error. I have installed all of the librsvg packages along with cairo. I cannot find anything online about what else to install to get it to work. I did hear that the rsvg module hasn't been updated since 2005 so is it just not compatible with Python 3.2, or is there something else I can try to install it? Alternatively, if rsvg does not work, does anyone have any suggestions for a simple way to display an SVG file through Python (basically just show the image)?

EDIT: The error I get is: 'ImportError: No module named rsvg'

This error does not show in python2

Thanks in advance

Leitmotif answered 1/5, 2012 at 4:50 Comment(3)
What error are you getting? Can you add it to your question?Polacre
I think PyQt should be capable of displaying an SVG image. It works with Python 3 - look for the package python3-pyqt4.Mohawk
You could wrap it through ctypes, see https://mcmap.net/q/1330911/-error-with-python-ctypes-and-librsvg. I have to install a huge amount of packages in order to get the python bindings for it, so I just use ctypes when I rarely need it.Jarv
B
18

I experienced a lot of difficulty trying to figure out how to do this. I hope others find this answer and save themselves a lot of time!

For Python 3, Python language bindings for several libraries originally written in C (including GTK, Clutter, and librsvg) have been replaced by GObject introspection libraries, Python code which dynamically generates Python objects from C "objects".

In order to use librsvg on Python 3, first install the necessary GObject introspection libraries (in addition to the Python 3 Cairo library). For example, on Ubuntu 13.10:

sudo apt-get install gir1.2-rsvg-2.0 python3-cairo python-gi-cairo python3-gi

Then test it out with the following code.

#!/usr/bin/env python3                                                          

# `gi.repository` is a special Python package that dynamically generates objects
import gi
gi.require_version('Rsvg', '2.0')
from gi.repository import Rsvg
import cairo

INPUTFILE = 'tiger.svg'

if __name__ == '__main__':
    # create the cairo context                                                  
    surface = cairo.SVGSurface('myoutput.svg', 580, 530)
    context = cairo.Context(surface)

    # use rsvg to render the cairo context                                      
    handle = Rsvg.Handle()
    svg = handle.new_from_file(INPUTFILE)
    svg.render_cairo(context)

In order to implement this for your project,

  1. change cairo.SVGSurface to be whatever surface you are going to draw on, and
  2. modify the value of INPUTFILE to be the name of the SVG file you wish to render.
Biel answered 2/11, 2013 at 16:27 Comment(2)
Also sudo apt-get install python3-gi and, in Python: from gi import require_version and require_version('Rsvg', '2.0') before from gi.repository import Rsvg.Candytuft
On Ubuntu 18, even with all those packages installed, I get the error, ModuleNotFoundError: No module named 'gi'Rafaellle

© 2022 - 2024 — McMap. All rights reserved.