python-3.x 将.SVG图像放入tkinter Frame

5lhxktic  于 12个月前  发布在  Python
关注(0)|答案(4)|浏览(147)

我一直在尝试把图像从https://betacssjs.chesscomfiles.com/bundles/web/favicons/safari-pinned-tab.f387b3f2.svg到Tkinter帧。我从here的帖子中发现,在rsvg和cairo的帮助下是可能的。
我在Windows 10上使用Python 3.6。我从here获取rsvg,从here获取cairo,然后将文件夹解压缩到'C:\Users.\site_packages'文件夹。它们很好,但我不知道如何使用它们。我试着使用代码:

import tkinter as tk
main=tk.Tk()
frame=tk.Frame(main)
def svgPhotoImage(self,file_path_name):
        from PIL import Image,ImageTk
        import rsvg,cairo 
        svg = rsvg.Handle(file=file_path_name)
        width, height = svg.get_dimension_data()[:2]
            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
            context = cairo.Context(surface)
            #context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
            svg.render_cairo(context)
            tk_image=ImageTk.PhotoImage('RGBA')
            image=Image.frombuffer('RGBA',(width,height),surface.get_data(),'raw','BGRA',0,1)
            tk_image.paste(image)
            return(tk_image)
    tk_image=self.svgPhotoImage(filename)
    frame.configure(image=tk_image)

#rsvg.py
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)

        rsvg = rsvgClass()).
h = rsvg.Handle("box.svg")
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
ctx = cairo.Context(s)
h.render_cairo(ctx)

在尝试了这些脚本之后,我一直收到错误消息:

AttributeError: module 'rsvg' has no attribute 'Handle'

我确信我在这个过程中做错了什么,但经过几个小时的搜索仍然不能想出如何让它工作。我还尝试安装Pycairo(通过PIP),但得到了错误消息

ERROR: Command "'c:\...\python36-32\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\...\\pip-install-peqhj3x1\\pycairo\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\...\Temp\pip-record-jfozfbuc\install-record.txt' --single-version-externally-managed --compile" failed with error code 1 in C:\...\pip-install-peqhj3x1\pycairo\

我不知道现在该怎么办
编辑:我终于能够从https://www.lfd.uci.edu/~gohlke/pythonlibs/获得pycairo,现在它可以工作了。我终于得到了rsvg不给给予我的错误消息以上参考here和得到必要的DLL文件从here。**Cairo工作正常,但RSVG正在创建一个空白屏幕。**我尝试了另一个7行here,并得到一个空白文件的输出,而不是转换后的文件。显然RSVG是不工作,我认为这是一个安装问题(如。不正确的.dll)。帮忙吗?
如果我尝试使用cairo和rsvg,因为它们占用的空间很小,而且速度很快; wand或其他库不是一个选择。我只是想能够把一个SVG文件到一个tkinter框架。如果有人知道如何正确安装rsvg,我会很感激知道。
任何帮助都将不胜感激。谢谢你的任何建议。

lvjbypge

lvjbypge1#

我使用svglib实现了这一点:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF, renderPM

drawing = svg2rlg("safari-pinned-tab.f387b3f2.svg")
renderPM.drawToFile(drawing, "temp.png", fmt="PNG")

from tkinter import *

tk = Tk()

from PIL import Image, ImageTk

img = Image.open('temp.png')
pimg = ImageTk.PhotoImage(img)
size = img.size

frame = Canvas(tk, width=size[0], height=size[1])
frame.pack()
frame.create_image(0,0,anchor='nw',image=pimg)

tk.mainloop()
ruoxqz4g

ruoxqz4g2#

我终于成功地在Windows上用Python 3和PyCairo转换SVG,但没有rsvg。Rsvg仍然不会合作,但显然仍然可以在没有它的情况下加载SVG。
安装过程并不是很简单,但嘿,它的工作!

说明:

请注意,这些说明是特定于Windows的。在Linux上,可以简单地通过pip安装pycairo并使用PyGObject的rsvg。
1.从https://www.lfd.uci.edu/~gohlke/pythonlibs/下载用于Windows安装的pycairo .whl文件。
1.通过pip安装下载的文件:pip install /path/to/your/pycairo/whl.whl如果这不起作用,请尝试使用上述链接中的其他文件。可能需要几次尝试。
1.运行pip install tinycss cssselect2 defusedxml。这将为下一步安装依赖项。
1.转到https://github.com/Kozea/CairoSVG。下载cairosvg文件夹并将其解压缩到站点包文件夹,使代码能够导入它。
1.转到cairosvg文件夹并打开文件surface.py
1.查找显示import cairocffi as cairo的行,并将其替换为import cairo
这应该足以在没有C++编译器的情况下在Windows上使用PyCairo。不幸的是,这仍然没有解决rsvg的问题(相反,它取代了rsvg),但至少它工作。

以下是一些如何使用它的例子:

将SVG转换为PNG:

import cairosvg
cairosvg.svg2png(url="example.svg", write_to="output.png")

将SVG放入Tkinter窗口而不下载输出:

import cairosvg
import io
import tkinter as tk
from PIL import Image,ImageTk

main=tk.Tk()

image_data = cairosvg.svg2png(url="example.svg")
image = Image.open(io.BytesIO(image_data))
tk_image = ImageTk.PhotoImage(image)

button=tk.Label(main, image=tk_image)
button.pack(expand=True, fill="both")
main.mainloop()

因为它仍然使用Cairo,所以这个解决方案非常快,并且几乎没有依赖性。
希望这个答案对将来阅读这篇文章的人有用!

b1zrtrql

b1zrtrql3#

下面是另一种方法:

from pylunasvg import Document
import numpy as np
from urllib import request
from PIL import Image

contents = request.urlopen("https://betacssjs.chesscomfiles.com/bundles/web/favicons/safari-pinned-tab.f387b3f2.svg").read()

document = Document.loadFromData(contents)
bitmap = document.renderToBitmap()

svgArray = np.array(bitmap, copy=False)
img = Image.fromarray(svgArray)

# Insert tkinter code here

免责声明:我为库编写了绑定,这里是github链接:pylunasvg并链接到用c++编写的原始库:lunasvg

r9f1avp5

r9f1avp54#

感谢j_4321在此提供的答案https://stackoverflow.com/a/64829808/870802
我只是在这里发出一个稍微不同的光,以防它对任何人都有帮助。
据我所知,tksvg是在tkinter 8.6中可视化SVG图形的最简单和最简单的方法。我可以在IDLE中做到以下几点:

python -m pip install tksvg    ---> this is not in IDLE actually, but in cmd!

import tkinter as tk
import tksvg

window = tk.Tk()

d = '''<svg width="0" height="0"><path d="M17.2 26.6l1.4 2.9 3.2.5-2.2 2.3.6 3.2-2.9-1.5-2.9 1.5.6-3.2-2.3-2.3 3.2-.5zM44.8 3.8l2 .3-1.4 1.4.3 2-1.8-.9-1.8.9.3-2L41 4.1l2-.3.9-1.8zM8.9 10l.9 1.8 2 .3-1.4 1.4.3 2-1.8-.9-1.8.9.3-2L6 12.1l2-.3zm37.6 17l.647 1.424 1.553.258-1.165 1.165.26 1.553-1.424-.776-1.295.647.26-1.553-1.165-1.165 1.553-.259zM29.191 1C24.481 2.216 21 6.512 21 11.626c0 6.058 4.887 10.97 10.915 10.97 3.79 0 7.128-1.941 9.085-4.888-.87.224-1.783.344-2.724.344-6.028 0-10.915-4.912-10.915-10.97 0-2.25.674-4.341 1.83-6.082z" fill="#7694b4" fill-rule="evenodd"></path></svg>'''

# if you have a SVG file on disk, use the file parameter:
# svg_image = tksvg.SvgImage( file = 'path/to/file' )

# if you have SVG code from somewhere - in my case I scraped it from a website - use the data parameter:
svg_image = tksvg.SvgImage( data = d )

# You can also resize the image, but using only one of the three available parameters:
# svg_image = tksvg.SvgImage( data = d, scale = 0.5 )
# svg_image = tksvg.SvgImage( data = d, scaletowidth = 200 )
svg_image = tksvg.SvgImage( data = d, scaletoheight = 200 )

tk.Label( image = svg_image ).pack()

最终结果是:

如果您将SVG源代码保存在手边,那么您总是可以通过生成一个新的图像来调整图像的大小。
版本:Windows 7x64,Python 3.8.10,tkinter 8.6.9.0,tksvg 0.7.4

相关问题