1

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.

user3541250
  • 13
  • 1
  • 4
  • 2
    I believe it was answered in [this post](http://stackoverflow.com/questions/14129405/python-pyinstaller-and-include-icon-file) http://stackoverflow.com/questions/14129405/python-pyinstaller-and-include-icon-file – jrasm91 May 23 '14 at 23:40
  • @Jason thats awesome ... I use pyinstaller all the time and had no idea about that – Joran Beasley May 23 '14 at 23:58

2 Answers2

3

You can include any reasonably sized file in any script just by base64 encoding it. Then you store it as a string.

Thomas Dignan
  • 7,052
  • 3
  • 40
  • 48
  • 4
    +1 since this is 100% correct ... however you could at least link him to something that would help him further (such as http://code.activestate.com/recipes/52264-inline-gifs-with-tkinter/) – Joran Beasley May 23 '14 at 23:52
1

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)))
sebdelsol
  • 1,045
  • 8
  • 15