Reputation: 946
I am trying to add a gif into my single .exe
so I don't have to have them open the exe with the gif in the same location. I am trying to include the gif into the program so they can open the program wherever they would like and the gif still be in the program.
Here is what I am pasting into my cmd to create my single
C:\Python27\Scripts\pyinstaller.exe --onefile --windowed --icon=fav.ico Program.pyw
is there another --
command I can do to include the gif?
For example:
--includeFile=MYgif.gif
Upvotes: 1
Views: 2085
Reputation: 709
You'll need to edit the spec file generated by pyinstaller and add the image to it.
When you use pyinstaller, it outputs a .spec file which is a configuration file. If you wanted to build your program again using the same options, you can just use 'pyinstaller myprogram.spec' instead of 'pyinstaller myprogram.py'
So open up the spec file in a text editor, and put your images in the 'datas' list like this.
datas=[('my_image1.gif', ''),
('my_image2.gif', ''),
('my_image3.gif', ''),
('my_image4.gif', '')],
During the execution of a onefile program created with pyinstaller the data files are unpacked to a temp directory. So to access your img files from within your program use a function like this.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
(return os.path.join(base_path, relative_path)
I grabbed that function from this link Bundling data files with PyInstaller (--onefile)
Now build your program using your edited spec file. 'pyinstaller myProgram.spec'
Upvotes: 1
Reputation: 2391
I would highly recommend to use base64.b64encode()
to convert the gif into a string and embed it into a py
file, then just import
this file in your main script, pyinstaller
would automatically pack this file into your exe.
import base64
# Encode your gif into a string
img_encoded = base64.b64encode(open('example.gif', 'rb').read())
# It looks like this: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcG ......
# Then embed this string into mygif.py,
# and in your main script: import mygif
# Decode the image into binary content
img = base64.b64decode(img_encoded)
If the image is jpeg or png and you happen to use wxPython
for GUI, there's also a very convenient img2py
to use. But I assume your gif file is animated, then you might have to encode each frame to keep all image data should you use img2py
.
Upvotes: 0