如何在Tkinter标签中使用base64编码的图像字符串? [英] How do I use the base64 encoded image string in Tkinter label?
问题描述
我正在编写一个tkinter程序,该程序利用一些JPG文件作为背景.但是,我发现,当使用"pyinstaller"将脚本转换为.exe文件时,用于tkinter Windows的图像未编译/添加到.exe文件中.
I am writing a tkinter program that utilises some JPG files for its background. 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.
因此,我决定在Python脚本中对图像进行硬编码,以便没有外部依赖性.为此,我做了以下事情:
Therefore, I have decided to hardcode the image in the Python script so that there is no external dependency. For this purpose, I have done the following things:
import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)
以上代码用于解码基本64位编码的Image数据. 我想将解码后的图像数据用作图片,并在tkinter中显示为标签/按钮.
The above code is used for decoding the base 64 encoded Image data. I want to use this decoded image data to be used as a picture and display as a label/button in tkinter.
例如:
from tkinter import *
root=Tk()
l=Label(root,image=image=PhotoImage(data=datas)).pack()
root.mainloop()
但是,tkinter不接受存储在data
中的值用作图像.
它显示以下错误-
However, tkinter is not accepting the value stored in data
to be used as an image.
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
类(在tk 8.6的Python 3中)只能读取GIF,PGM/PPM和PNG图像格式.有两种读取图像的方法:
Tkinter PhotoImage
class (in Python 3 with tk 8.6) can only read GIF, PGM/PPM and PNG image formats. There are two ways to read an image:
- 从文件中:
PhotoImage(file="path/to/image.png")
- 来自base64编码的字符串:
PhotoImage(data=image_data_base64_encoded_string)
- From a file:
PhotoImage(file="path/to/image.png")
- From a base64-encoded string:
PhotoImage(data=image_data_base64_encoded_string)
首先,如果要将图像转换为base64编码的字符串:
First, if you want to convert an image into a base64-encoded string:
import base64
with open("path/to/image.png", "rb") as image_file:
image_data_base64_encoded_string = base64.b64encode(image_file.read())
然后在Tkinter中使用它:
Then use it in Tkinter:
import tkinter as tk
root = tk.Tk()
im = PhotoImage(data=image_data_base64_encoded_string)
tk.Label(root, image=im).pack()
root.mainloop()
我认为您的问题是,在应该直接使用base64_encodedString
的情况下,在PhotoImage
中使用该字符串之前,先用datas= base64.b64decode(base64_encodedString)
对该字符串进行了解码.
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.
这篇关于如何在Tkinter标签中使用base64编码的图像字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!