I have a Python script that I compile with Pyinstaller into one single .exe file. Unfortunately, both the script and the compiled file must be in the same directory as my .ico and background (.png) image, as I refer to them like this:
root.iconbitmap("logo.ico")
background = ImageTk.PhotoImage(Image.open("background.png"))
It is possible to include the picture data in the script file itself, instead of make it depended on a file outside the single executable file? I'm using Tkinter and PIL.
解决方案
As suggested, you could base64 encode it:
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))
Then :
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)))