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

Viewed 285

I'm having trouble with my pyinstaller executable, when I launch it I get this error:

enter image description here

The problem is due to the exe not being able to find an image file 'icon.ico' because pyinstaller didn't package the file inside the executable file. What I'm asking is how I would package the icon inside the EXE file and what the directory for that icon file would be after it has been packaged. The image file is being used as an icon for a Tkinter GUI.

This is the code for the Tkinter app:

app = Tk()
app.title('MagnetMagnet - RARBG Scraper')
app.iconbitmap(r'icon.ico')
app.geometry('500x225')

app.mainloop()
2 Answers

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.

I think you don't have to give the icon in the python file. instead type pyinstaller -i iconfile pythonfile in your terminal(Windows)

Related