how to include images in the exe file while making exe with pyc.py

1k Views Asked by At

while making the exe with pyc.py, it is just referencing the images as static folder reference.
Actually i have designed gui using the iron python in visual studio. i have an image which has to be displayed in the first frame. So i have done it by using the code

MyButton.Image = Image.FromFile("stringsInfo.png")


Now i have run the project and it showed successfully. Now i built an exe using the pyc module. But here comes the problem is, it not showing any window becoz the reference image is not in the folder where the exe is located.
This is my problem. I want the solution as i can simply include the image in the exe and simply access it. If it is java, we can get the image in the jar like this right.

ImageIcon image = new ImageIcon(this.getClass().getResource("/package/sub_package/image_name.png"));


As same, i want the image to be in exe file and refer from it. Just somebody say me any solution. Thanks in advance...

1

There are 1 best solutions below

0
On

No need to add folder. You can convert the image into base64 encoding string and add these strings as variable in project.

You can convert byte64 to image in runtime.

Sample code

with open("samplefile.png", "rb") as image_file: encoded_string = base64.b64encode(image_file.read())

Only once you need to convert. Store this as a variable in python file.

Use same variable as reference to your Image.

All the best.