我有一个Python脚本,可以将Pyinstaller编译成一个单独的.exe文件。不幸的是,脚本和编译文件都必须与.ico和背景(.png)图像位于同一目录中,因为我这样引用它们:
root.iconbitmap("logo.ico")
background = ImageTk.PhotoImage(Image.open("background.png"))
是否可以将图片数据包含在脚本文件本身中,而不是使其依赖于单个可执行文件之外的文件?我正在使用Tkinter和PIL。
如建议的那样,您可以对它进行base64编码:
import base64
im_filename = 'background.png'
im_variableName = 'background'
py_filename = 'embeddedImage.py'
with open(im_filename,'rb') as f:
str64 = base64.b64encode(f.read())
with open(py_filename,'w') as f:
f.write('%s="%s"'%(im_variable_name,str64))
然后 :
from PIL import Image
import cStringIO
import base64
from embeddedImage import background
# or copy paste the background variable found in embeddedImage.py
im = Image.open(cStringIO.StringIO(base64.b64decode(background)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句