Search code examples
pythonpython-3.xcairopycairorsvg

Problem when using RSVG and Cairo in Python


I have been trying to put a SVG image in a tkinter frame with python 3.6 on Windows. I recently downloaded a the pycairo‑1.18.0‑cp36‑cp36m‑win32.whl file from https://www.lfd.uci.edu/~gohlke/pythonlibs/ and in the command prompt typed pip install pycairo‑1.18.0‑cp36‑cp36m‑win32.whl to install it. This is for pycairo, and it works fine.

I also need to install rsvg. Because it is unavailable for windows, I saved the following as rsvg.py in the same folder as my script:

#some code to give rsvg.render_cairo(ctx) ability
#on windows.
import os
try:
    import rsvg
    WINDOWS=False
except ImportError:
    print"Warning, could not import 'rsvg'"
    if os.name == 'nt':
        print "Detected windows, creating rsvg."
        #some workarounds for windows

        from ctypes import *

        l=CDLL('librsvg-2-2.dll')
        g=CDLL('libgobject-2.0-0.dll')
        g.g_type_init()

        class rsvgHandle():
            class RsvgDimensionData(Structure):
                _fields_ = [("width", c_int),
                            ("height", c_int),
                            ("em",c_double),
                            ("ex",c_double)]

            class PycairoContext(Structure):
                _fields_ = [("PyObject_HEAD", c_byte * object.__basicsize__),
                            ("ctx", c_void_p),
                            ("base", c_void_p)]

            def __init__(self, path):
                self.path = path
                error = ''
                self.handle = l.rsvg_handle_new_from_file(self.path,error)


            def get_dimension_data(self):
                svgDim = self.RsvgDimensionData()
                l.rsvg_handle_get_dimensions(self.handle,byref(svgDim))
                return (svgDim.width,svgDim.height)

            def render_cairo(self, ctx):
                ctx.save()
                z = self.PycairoContext.from_address(id(ctx))
                l.rsvg_handle_render_cairo(self.handle, z.ctx)
                ctx.restore()



        class rsvgClass():
            def Handle(self,file):
                return rsvgHandle(file)

This was my script:

from rsvg import *    
rC = rsvgClass()
h = rC.Handle("YOUR-FILE-HERE.svg")
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)
h.render_cairo(ctx)

When I run it, I get the message:

Traceback (most recent call last): l=CDLL('librsvg-2-2.dll') File "C:\Users\Whoever\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 348, in __init__ self._handle = _dlopen(self._name, mode) OSError: [WinError 126] The specified module could not be found

Pretty much the librsvg-2-2.dll file is not there. Now what?

Any help would be greatly appreciated


Solution

  • This is pretty low spread on the internet and I'm posting this to help anyone else with the same question. After tons of searching I found this: https://github.com/jmcb/python-rsvg-dependencies/tree/master/bin. Simply copy all the .dll files into the same folder as the script and it should work.