我一直试图将SVG图像放在带有python3.6的tkinter框架中。我最近从pycairo‑1.18.0‑cp36‑cp36m‑win32.whl下载了一个https://www.lfd.uci.edu/~gohlke/pythonlibs/文件,并在命令提示符下输入了pip install pycairo‑1.18.0‑cp36‑cp36m‑win32.whl来安装它。这是给皮凯罗的,很好用。
我还需要安装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几乎所有的labsvg-2-2.dll文件都不存在。这次又是什么?
任何帮助都将不胜感激。
发布于 2019-06-03 15:02:47
这是相当低的传播在互联网上,我张贴这是为了帮助其他人有同样的问题。经过大量的搜索,我发现了这个:https://github.com/jmcb/python-rsvg-dependencies/tree/master/bin。只需将所有.dll文件复制到与脚本相同的文件夹中,它就会正常工作。
https://stackoverflow.com/questions/56207726
复制相似问题