[英]How do I use the base64 encoded image string in Tkinter label?
I am writing a tkinter program that utilises some JPG files for its background. 我正在编写一个tkinter程序,该程序利用一些JPG文件作为背景。 However, I have found that when the script is converted to an .exe file using "pyinstaller", the images used for tkinter windows are not compiled/added to the .exe file. 但是,我发现,当使用“ pyinstaller”将脚本转换为.exe文件时,用于tkinter Windows的图像未编译/添加到.exe文件中。
Therefore, I have decided to hardcode the image in the Python script so that there is no external dependency. 因此,我决定在Python脚本中对图像进行硬编码,以便没有外部依赖性。 For this purpose, I have done the following things: 为此,我做了以下事情:
import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)
The above code is used for decoding the base 64 encoded Image data. 上面的代码用于解码base 64编码的Image数据。 I want to use this decoded image data to be used as a picture and display as a label/button in tkinter. 我想将此解码的图像数据用作图片,并在tkinter中显示为标签/按钮。
For example: 例如:
from tkinter import *
root=Tk()
l=Label(root,image=image=PhotoImage(data=datas)).pack()
root.mainloop()
However, tkinter is not accepting the value stored in data
to be used as an image. 但是,tkinter不接受存储在data
的值作为图像。 It displays the following error - 它显示以下错误-
Traceback (most recent call last):
File "test.py", line 23, in <module>
l=Label(root,image=PhotoImage(data=datas))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3394, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 3350, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
Tkinter PhotoImage
class (in Python 3 with tk 8.6) can only read GIF, PGM/PPM and PNG image formats. Tkinter PhotoImage
类(在带tk 8.6的Python 3中)只能读取GIF,PGM / PPM和PNG图像格式。 There are two ways to read an image: 有两种读取图像的方法:
PhotoImage(file="path/to/image.png")
来自文件: PhotoImage(file="path/to/image.png")
PhotoImage(data=image_data_base64_encoded_string)
从base64编码的字符串中: PhotoImage(data=image_data_base64_encoded_string)
First, if you want to convert an image into a base64-encoded string: 首先,如果要将图像转换为base64编码的字符串:
import base64
with open("path/to/image.png", "rb") as image_file:
image_data_base64_encoded_string = base64.b64encode(image_file.read())
Then use it in Tkinter: 然后在Tkinter中使用它:
import tkinter as tk
root = tk.Tk()
im = PhotoImage(data=image_data_base64_encoded_string)
tk.Label(root, image=im).pack()
root.mainloop()
I think that your problem is that you decoded the string with datas= base64.b64decode(base64_encodedString)
before using it in PhotoImage
while you should have used base64_encodedString
directly. 我认为您的问题是,在PhotoImage
使用字符串之前,已使用datas= base64.b64decode(base64_encodedString)
对该字符串进行了解码,而您应该直接使用base64_encodedString
。
Just to correct the very good answer of j_4321, the correct line for PhotoImage
is : 只是为了纠正j_4321的很好的答案,正确的路线PhotoImage
是:
im = tk.PhotoImage(data=image_data_base64_encoded_string)
and my solution to write the 'image' string in order to import it after : 和我的解决方案来写'image'字符串,以便在之后导入:
with open("image.py", "wb") as fichier:
fichier.write(b'imageData=b\'' + image_data_base64_encoded_string + b'\'')
A simple import image as img
and the image data will be stored in the .exe file with Pyinstaller ( -F
option). 一个简单的import image as img
和图像数据将通过Pyinstaller( -F
选项)存储在.exe文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.