在Python中使用RSVG和Cairo时出现问题

时间:2019-05-19 12:17:31

标签: python python-3.x cairo pycairo rsvg

我一直试图在Windows上使用python 3.6将SVG图像放入tkinter框架中。我最近从https://www.lfd.uci.edu/~gohlke/pythonlibs/下载了一个pycairo‑1.18.0‑cp36‑cp36m‑win32.whl文件,并在命令提示符下键入pip install pycairo‑1.18.0‑cp36‑cp36m‑win32.whl进行安装。这是用于pycairo的,它工作正常。

我还需要安装rsvg。由于Windows无法使用该文件,因此将以下文件作为rsvg.py保存在与脚本相同的文件夹中:

#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)

这是我的脚本:

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)

运行它时,我收到消息:

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

几乎没有librsvg-2-2.dll文件。现在呢?

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

这在互联网上的传播非常低,我将其发布给其他有相同问题的人。经过大量搜索,我发现了这个:https://github.com/jmcb/python-rsvg-dependencies/tree/master/bin。只需将所有.dll文件复制到脚本所在的文件夹中,即可正常工作。