[英]Base64 string to image in tkinter
I have a image i'm using in my GUI and i dont want it as an external resource when i compile the .exe or to my .py. 我有一个在GUI中使用的图像,当我编译.exe或.py时,我不希望它作为外部资源。 I figured i should encode it to a string, copy the string in the code and decode it and serve it to Tkinter.
我想我应该将其编码为字符串,将字符串复制为代码并解码,然后将其提供给Tkinter。 Tried a bunch of solutions but still doesnt work, here is the code:
尝试了一堆解决方案,但仍然无法正常工作,下面是代码:
import tkinter as tk
from tkinter import filedialog
from tkinter import *
import PIL
from PIL import Image, ImageTk
import base64
stringimagine="something the print gave me"
imagine=open('logo.jpg','rb')
encoded=base64.b64encode(imagine.read())
print(encoded)
imagine2=base64.b64decode(stringimagine)
fereastra_principala = tk.Tk()
poza=Label(fereastra_principala,image=imagine2)
poza.pack(fill='both',expand='yes')
fereastra_principala.mainloop()
to this code i receive this error: 到此代码,我收到此错误:
File "C:\Python34\lib\tkinter\__init__.py", line 2609, in __init__ Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2127, in __init__ (widgetName, self._w) + extra + self._options(cnf))_tkinter.TclError
And this is how i use to get the photo now, as an external resource: 这就是我现在用来作为外部资源获取照片的方式:
img=Image.open('logo.jpg')
image=ImageTk.PhotoImage(img)
poza=Label(fereastra_principala,image=image)
poza.pack()
If you have base64-encoded data, you need to use the data
argument. 如果您具有base64编码的数据,则需要使用
data
参数。 However, I don't think this will work for jpg. 但是,我认为这不适用于jpg。 It will work for .gif images though.
它将适用于.gif图像。
This is what the canonical documentation says for the data option: 这是规范文档对data选项的说明:
Specifies the contents of the image as a string.
将图像的内容指定为字符串。 The string should contain binary data or, for some formats, base64-encoded data (this is currently guaranteed to be supported for GIF images).
该字符串应包含二进制数据,或者对于某些格式,应包含base64编码的数据(目前保证GIF图像支持该数据)。 The format of the string must be one of those for which there is an image file format handler that will accept string data.
字符串的格式必须是其中一种具有接受字符串数据的图像文件格式处理程序的格式。 If both the -data and -file options are specified, the -file option takes precedence.
如果同时指定了-data和-file选项,则-file选项优先。
import Tkinter as tk
IMAGE_DATA = '''
R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA\nAAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
TgceZJllw4pd2Xpagq0WfeYrD7\n2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7\n
'''
root = tk.Tk()
image = tk.PhotoImage(data=IMAGE_DATA)
label = tk.Label(root, image=image, padx=20, pady=20)
label.pack()
root.mainloop()
Got a way: 有办法:
Say image='abc' is the string of the image coded in b64. 假设image ='abc'是b64中编码的图像的字符串。
def install():
if not os.path.isfile("logo.jpg"):
open('logo.jpg','wb').write(base64.b64decode(image))
Gonna run this in my main() function and if the image doesnt exist in the folder, it will be created. 要在我的main()函数中运行该文件,如果该文件夹中不存在该图像,则会创建该图像。 Afterwords you'll just load the image as normal:
之后,您将照常加载图像:
self.img=Image.open('logo.jpg')
self.image=ImageTk.PhotoImage(self.img)
I havent found a solution not to save the image at all however. 我还没有找到根本不保存图像的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.