Pyinstaller and --Onefile: How to Include an Image in the Exe File

Pyinstaller and --onefile: How to include an image in the exe file

Edit:

I belive I found the solution to my problem.

# -*- mode: python -*-
a = Analysis(['AMOS_Visualizer.py'],
pathex=['C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting'],
hiddenimports=[],
hookspath=None,
runtime_hooks=None)

for d in a.datas:
if 'pyconfig' in d[0]:
a.datas.remove(d)
break

a.datas += [('Logo.png','C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\Logo.png', 'Data')]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='AMOS_Visualizer.exe',
debug=False,
strip=None,
upx=True,
console=True, icon='C:\\Users\\elu\\PycharmProjects\\Prosjektet\\Forsok splitting\\AMOS.ico')

And adding the following to my main.py script

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)

Logo = resource_path("Logo.png")

How to use pyinstaller to include images in the .exe

If you dont use the --onefile command, you can just drag and drop the images into the folder your exe is in. Or drop in the folder of images.

If you do use onefile, you need to modify the .spec file you get when you run the code pyinstaller script.py. Then run pyinstaller scriptname.spec. edit the datas variable somewhat like this

datas = [('src/image.png', '.'),
('src/image1.png' '.')]

If the images are in a different folder than your script you put the address of where they're at in regards to the script.

more information can be read in the documentation

Including an image in a PyInstaller onefile executable

Okay well I was sure I had already tried this, but by adding the resource path method from Pyinstaller and --onefile: How to include an image in the exe file, but without changing the spec file, this command works:

pyinstaller --noconfirm --onefile --console --add-data "[workspace]/src/logo.png:."  "[workspace]/src/gui.py"

Pyinstaller embed images folder in --onefile --windowed application

When you want to embed a file into your executable, you need to do two steps:

First, add it with add-data to your executable (as you already did). Second, load the file from the extracted path on runtime.

You are trying to load the file from images/myicon.png, and the path is next to your executable. Still, the file is not there, and meanwhile, the runtime files would be extracted in a temp directory, e.g. C:/Users/<user>/AppData/Local/Temp/_MEIXXX. So it needs to be loaded from that directory.

You can use sys._MEIPASS to get the temp path where the extracted files are located. Moreover, you can create a function to load external files:

import os
import sys

def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)

self.SetIcon(wx.Icon(resource_path("images/myicon.png")))

How to include image in Kivy application with onefile mode pyinstaller?

The problem is with your attempt to fix the path:

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(
sys,
'_MEIPASS',
os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)

path = resource_path("say.png")

just replace that code with:

import kivy.resources

if getattr(sys, 'frozen', False):
# this is a Pyinstaller bundle
kivy.resources.resource_add_path(sys._MEIPASS)

How do I add an image into a onefile pyinstaller executable?

An easy way is save the Bytes of the Image,and when open it,Save the picture in your PC,and use app.iconbitmap(r'icon.ico').

Firstly,use open to get the Image Bytes:

with open('icon.ico','rb') as f:
ImageBytes = f.read()
print(ImageBytes)
# b'xxxxxxxxxxxxxxxxxx'

Then your all code should be:

ImageBytes = b'xxxxxxxxxxxxxxxxxx'
with open('icon.ico','wb') as f:
f.write(ImageBytes)
app = Tk()
app.title('MagnetMagnet - RARBG Scraper')
app.iconbitmap(r'icon.ico')
app.geometry('500x225')
app.mainloop()

When you open this exe file,it will generate a new ico Image,you can delete it.And it will generate a new one again next time when you open it.

How to include images in one file with python pyinstaller

First, there is no need to add your executable icon as a data, putting it in icon param would be enough.

Next, when you add a data to PyInstaller it would bring your data and extract it in temp folder (e.g. C:\Users\Rat's Nest\Appdata\local\temp\_MEIXXXX\) so you need to change your code to open your files from that directory. A good practice is to use this function in your code to retrieve your data. When running executable sys._MEIPASS would be equal to the PyInstaller temp folder.

def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)

Then you can use it with something like new_source = resource_path("clogo.png").



Related Topics



Leave a reply



Submit