Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Tkinter标签中使用base64编码的图像字符串?_Python_Image_Tkinter_Base64_Pyinstaller - Fatal编程技术网

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

Python 如何在Tkinter标签中使用base64编码的图像字符串?,python,image,tkinter,base64,pyinstaller,Python,Image,Tkinter,Base64,Pyinstaller,我正在写一个tkinter程序,它使用一些JPG文件作为背景。但是,我发现,当使用“pyinstaller”将脚本转换为.exe文件时,用于tkinter窗口的图像不会编译/添加到.exe文件中 因此,我决定在Python脚本中硬编码图像,这样就没有外部依赖。为此,我做了以下几件事: import base64 base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) ''' datas= base64.b64decode(base

我正在写一个tkinter程序,它使用一些JPG文件作为背景。但是,我发现,当使用“pyinstaller”将脚本转换为.exe文件时,用于tkinter窗口的图像不会编译/添加到.exe文件中

因此,我决定在Python脚本中硬编码图像,这样就没有外部依赖。为此,我做了以下几件事:

import base64
base64_encodedString= ''' b'hAnNH65gHSJ ......(continues...) '''
datas= base64.b64decode(base64_encodedString)
上述代码用于解码基本64编码的图像数据。 我想使用解码后的图像数据作为图片,并在tkinter中显示为标签/按钮

例如:

from tkinter import *
root=Tk()
l=Label(root,image=image=PhotoImage(data=datas)).pack()
root.mainloop()
但是,tkinter不接受存储在
数据中的值作为图像使用。
它显示以下错误-

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
回溯(最近一次呼叫最后一次):
文件“test.py”,第23行,在
l=标签(根,图像=照片图像(数据=数据))
文件“C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\\ uuuuuuu init\uuuuuu.py”,第3394行,在\uuuu init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“C:\Users\Admin\AppData\Local\Programs\Python\Python35-32\lib\tkinter\\ uuuuuu init\uuuuuu.py”,第3350行,在uuu init中__
self.tk.call(('image','create',imgtype,name,)+选项)
_tkinter.TclError:无法识别图像数据

Tkinter
PhotoImage
类(在使用tk 8.6的Python 3中)只能读取GIF、PGM/PPM和PNG图像格式。有两种读取图像的方法:

  • 从文件:
    PhotoImage(file=“path/to/image.png”)
  • 从base64编码字符串:
    PhotoImage(data=image\u data\u base64\u encoded\u string)
首先,如果要将图像转换为base64编码字符串:

import base64

with open("path/to/image.png", "rb") as image_file:
    image_data_base64_encoded_string = base64.b64encode(image_file.read()) 
然后在Tkinter中使用它:

import tkinter as tk

root = tk.Tk()

im = tk.PhotoImage(data=image_data_base64_encoded_string)

tk.Label(root, image=im).pack()

root.mainloop()

我认为您的问题在于,在将字符串用于
PhotoImage
之前,您使用
datas=base64.b64decode(base64_encodedString)
对字符串进行了解码,而您应该直接使用
base64_encodedString

为了纠正j_4321的非常好的答案,
PhotoImage
的正确行是:

im = tk.PhotoImage(data=image_data_base64_encoded_string)
我的解决方案是写入“图像”字符串,以便在以下情况下导入:

with open("image.py", "wb") as fichier:
    fichier.write(b'imageData=b\'' + image_data_base64_encoded_string + b'\'')

一个简单的
导入图像作为img
,图像数据将通过Pyinstaller(
-F
选项)存储在.exe文件中。

您使用的是python2还是python3?根据这一点,使用python3似乎是可能的。@j_4321我正在使用python3。我已经检查了这个问题,但它似乎并没有解决我的问题。那么您是否尝试过@j_4321中给出的代码?是的,我尝试过,但没有任何帮助。除了base64编码技术之外,是否有其他方法将图像嵌入python脚本中,以便可以在tkinter GUI中使用?确实,错误正如您所猜测的那样,我使用解码后的值来创建图像对象。谢谢