简体   繁体   English

如何在Tkinter标签中使用base64编码的图像字符串?

[英]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: 有两种读取图像的方法:

  • From a file: PhotoImage(file="path/to/image.png") 来自文件: PhotoImage(file="path/to/image.png")
  • From a base64-encoded string: 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.

相关问题 Python,Sqlite-如何将存储的图像(作为Blob或base64字符串)放入tkinter标签上的sqlite DB中 - Python, Sqlite - how to put an image stored (either as Blob or base64 string) in a sqlite DB on a tkinter Label 如何在Django中获取图像的base64编码字符串? - How to get base64 encoded string of an image in django? 如何将PIL图像作为Base64编码的字符串 - How to get a PIL image as a Base64 encoded string 如何从werkzeug.datastructures将base64编码的字符串转换为可以与FileStorage一起使用的Stream? - How do I convert a base64 encoded string to a Stream that I can use with FileStorage from werkzeug.datastructures? 如何将 base64 编码图像与moviepy一起使用 - How to use base64 encoded image with moviepy 如何确定电子邮件是否为 Base64 编码? - How do I determine if an email is Base64 encoded? 在tkinter中Base64字符串到图像 - Base64 string to image in tkinter 调整大小/裁剪图像编码为base64图像字符串 - Resize/crop image encoded as base64 image string 是否有任何 Python 方法可以将 base64 编码的字符串转换为 Image - Is there any Python method to convert base64 encoded string to Image 我可以使用base64编码的字节字符串作为Django的秘密密钥吗? - Can I use a base64 encoded byte string as Django's secret key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM