在 Python 中使用 RSVG 和 Cairo 时出现问题
Problem when using RSVG and Cairo in Python
我一直在尝试将 SVG 图像放入 python 3.6 on Windows 的 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 文件。现在怎么办?
如有任何帮助,我们将不胜感激
这在互联网上的传播率很低,我发布它是为了帮助其他有同样问题的人。经过大量搜索,我发现了这个:https://github.com/jmcb/python-rsvg-dependencies/tree/master/bin。只需将所有 .dll 文件复制到与脚本相同的文件夹中,它应该可以工作。
我一直在尝试将 SVG 图像放入 python 3.6 on Windows 的 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 文件。现在怎么办?
如有任何帮助,我们将不胜感激
这在互联网上的传播率很低,我发布它是为了帮助其他有同样问题的人。经过大量搜索,我发现了这个:https://github.com/jmcb/python-rsvg-dependencies/tree/master/bin。只需将所有 .dll 文件复制到与脚本相同的文件夹中,它应该可以工作。