Maksiks
Maksiks

Reputation: 116

Python to exe but keeping the icon

I'm new to python and I don't know much yet. So, I wanted to convert my python project to an exe file, and so I followed the steps like in every youtube tutorial, terminal, pyinstaller and stuff (I use Pychram), but soon I realized that the icon I set for my exe isn't loading unless it's in the same folder as dist and .spec file. I decided to search some, I found a way on this site: https://clay-atlas.com/us/blog/2020/11/04/python-en-package-pyinstaller-picture/ (3rd method), but i was unable to decode it, I just need to replace something with my filename but I don't understand where exactly, like is pic2str a variable or a project name? Thanks. (Code might be slightly shifted in here copy from the website if you need)

import base64


def pic2str(file, functionName):
pic = open(file, 'rb')
content = '{} = {}\n'.format(functionName, base64.b64encode(pic.read()))
pic.close()

with open('pic2str.py', 'a') as f:
    f.write(content)


if __name__ == '__main__':
pic2str('test.png', 'explode')

And this:

import sys
import base64
from io import BytesIO
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from test import Ui_MainWindow
from PIL import Image, ImageQt

from pic2str import explode


class MainWindow(QtWidgets.QMainWindow):
  def __init__(self):
    super(MainWindow, self).__init__()
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)

    # Load byte data
    byte_data = base64.b64decode(explode)
    image_data = BytesIO(byte_data)
    image = Image.open(image_data)

    # PIL to QPixmap
    qImage = ImageQt.ImageQt(image)
    image = QPixmap.fromImage(qImage)

    # QPixmap to QLabel
    self.ui.label.setPixmap(image)
    self.ui.label.setScaledContents(True)


if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   window = MainWindow()
   window.show()
   sys.exit(app.exec_())

Upvotes: 1

Views: 898

Answers (2)

Maksiks
Maksiks

Reputation: 116

I found a solution, you just have to use

--onefile

so the .exe file doesn't require any connection to the folder, and use

--icon insertpicturename.ico

to make the file have the icon you need, you can convert an image to .ico using online converters. And, I would also recommend using

--noconsole

if it fits your needs, it just removes the console showing up before opening the file.

Upvotes: 1

Patrick
Patrick

Reputation: 1191

you can try auto-py-to-exe, its a convenient little script that will allow you to use pyinstaller from a GUI and it will create the required terminal command for you. you can get it using pip install auto-py-to-exe and launch it from your terminal with the command auto-py-to-exe, you will see a section called Icon where you can open a .ico file through a file broswer window

Upvotes: 0

Related Questions