【发布时间】:2019-05-19 12:17:30
【问题描述】:
我一直在尝试在 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 文件。现在呢?
任何帮助将不胜感激
【问题讨论】:
标签: python python-3.x cairo pycairo rsvg