Rsvg with Python 3.2 on Ubuntu

I am trying to use rsvg in Python 3.2, but I keep getting import error. I installed all librsvg packages along with cairo. I cannot find anything on the Internet about what else needs to be installed in order to make it work. I heard that the rsvg module has not been updated since 2005, as it is simply not compatible with Python 3.2, or is there something else that I can try installing? Otherwise, if rsvg does not work, does anyone have any suggestions on how to easily display the SVG file through Python (basically just showing the image)?

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

This error does not appear in python2

Thanks in advance

+4
source share
3 answers

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

For Python 3, the Python language bindings for several libraries originally written in C (including GTK , Clutter, and librsvg ) were replaced with the introspection GObject libraries , Python code that dynamically generates Python objects from C objects. "

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

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

Then check it with the following code.

 #!/usr/bin/env python3 # `gi.repository` is a special Python package that dynamically generates objects 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) 

To implement this for your project,

  • change cairo.SVGSurface to whatever surface you are going to paint on, and
  • change the INPUTFILE value to the name of the SVG file you want to display.
+8
source

To use the module in python 3 in Ubuntu, you usually need to install a specific python3 package that will look like python3-somemodule . A package called python-somemodule usually used only for python2.7, the current default version of Python in Ubuntu. In this case, it does not look, as long as there is a rsvg version for python3.

0
source

Change the code to Ubuntu 16.04 using Python 3.5 as an interpreter in Pycharm and get this result.

The file "myoutput.svg" is about twice the size of "tiger.svg" and there is no function from the original work.

myoutput.svg tiger.svg

0
source

Source: https://habr.com/ru/post/892706/


All Articles