4

I am building a Python application bundled with PyInstaller which is available for Windows, Linux (as AppImage) and MacOS.

I need shortcut icons for those. I have a logo, but I am struggling to bring it to the right format. It seems to me that all three platforms use different icon sizes and formats.

Is there any online/offline generator that allows me to create icons for all three platforms from a jpg file?

Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
Tom Atix
  • 381
  • 1
  • 21

4 Answers4

5

Tom! I found you the solution!.Go to here site and convert your image to icns format. Then in pyinstaller run the command:-

pyinstaller pythonfile.py -F --i=icon_name.icns -noconsole

It works fine.

And if you want to add icon to python.py in file in windows then try these:-

If you are using pygame use this code:-

icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

If you are using TKinter use this code:-

screen = Tk()

photo = PhotoImage(file = "Any image file")
screen.iconphoto(False, photo)

If you want to add icon after your file is completed use Pyinstaller:-

pip install pyinstaller

And in pyinstaller use this command:-

pyinstaller pythonfile.py -F --i icon_name.ico -noconsole

Some information about it:-

  • -F for 1 file so it doesn't create many files. Only 1 which will be the .exe file itself.
  • Go to this site for more info this site.
  • If you don't use GUI use this command: pyinstaller pythonfile.py -F --i icon_name.ico -nowindowed

Hope this helps you!

2

As you have mentioned you only need to convert the logo from jpg to icon files for the respective OS, then try image converter of Pillow. This code will generate icon files for all (Mac, Win, Linux)

from PIL import Image
filename = r'logo.jpg' # the directory of logo file
img = Image.open(filename) # reads the JPG file

# Generate and save the logo files
img.save('Win_logo.ico')   # On windows, icon file extension is .ico
img.save('Mac_logo.icns')  # On Mac OS, icon file extension is .icns
# Note: Creating icns requires Mac OS
img.save('Linux_logo.png') # in case if your AppImage requires any logo file

Optionally, you may specify the icon sizes (for Windows) you want:

icon_sizes = [(16,16), (32, 32), (48, 48), (64,64)]
img.save('logo.ico', sizes=icon_sizes)

The Pillow docs say that by default it will generate sizes [(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (255, 255)] and any size bigger than the original size or 255 will be ignored.

check the Pillow docs for more info.

as the Pyinstaller is OS based and ICNS is also generated OS based

More OS specified

from platform import system as OS_Name
os_name = OS_Name()
filename = r'logo.jpg' # the directory of logo file
img = Image.open(filename) # reads the JPG file

# Generate and save the logo files
if os_name == 'Windows': img.save('Win_logo.ico')   # On windows, icon file extension is .ico
if os_name == 'Darwin' : img.save('Mac_logo.icns')  # On Mac OS, icon file extension is .icns
# on Linux the os_name is 'Linux'
img.save('Linux_logo.png') # in case if your AppImage requires any logo file

OS independent for ICNS

The best os independent way is the Online way. Cloud Convert is a great tool for that. However, you must convert your jpg to png first. [Note: Always try to use png for logos as they have transparency and more convient] You may use the Above code's PNG part to auto-convert your JPG to PNG, or use JPG to PNG Finally convert your PNG file using PNG to ICNS.

Ratul Hasan
  • 544
  • 6
  • 14
  • Need to check if this works, if it does, it is exactly what I am looking for. But you mean img.save('Mac_logo.icns') right? It's a typo, the s is missing in the file extension? – Tom Atix Jun 02 '21 at 21:26
  • there are 2 extension `icn` and `icns`, as i don't have a mac, i'm a bit confused now. re-checking... – Ratul Hasan Jun 03 '21 at 05:04
  • Traceback (most recent call last): File "createicons.py", line 7, in img.save('Mac_logo.icns') # On Mac OS, icon file extension is .icns File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1983, in save save_handler = SAVE[format.upper()] KeyError: 'ICNS' – Tom Atix Jun 03 '21 at 13:59
  • as i commented in the code [was updated later] Just like pyinstaller, you will need Mac os to create Mac executable file and also the icns file. If you have Mac computer, then it should work file. OS specified stuffs really gives headache to the programmer. checking for any online api and posting new answer for Any OS – Ratul Hasan Jun 03 '21 at 19:14
  • Ah ok - so I'll just do it on mac and should be fine. – Tom Atix Jun 04 '21 at 00:24
1

It's easier if you just use auto-py-to-exe, it uses pyinstaller, but it auto-generates the command, and so you can just upload the image to a GUI

AG-88301
  • 127
  • 1
  • 10
0

If you want to convert your PNGs to an ICO format, you could use a PNG to ICO converter (online) like this. Since all platforms support .ICO format files, this would be your one-stop-shop.

The preferred icon size is a square of 96px x 96px (high quality) or 48px x 48px (medium quality).

Also, to set the icon to your application for with pyinstaller, you could specify the path for the icon while creating the installer using the command line.

pyinstaller --onefile --windowed --icon=app.ico app.py

This would create an installer which contains an application with the specified icon.

Just Mohit
  • 141
  • 1
  • 13
  • Does Mac OSX really also support these? Thought they need an extra format. Didn't know that. – Tom Atix May 31 '21 at 22:23
  • @TomAtix Does [this](https://stackoverflow.com/a/62607152/13108568) help you? – Just Mohit Jun 01 '21 at 03:19
  • so here it says .ico is only for windows, for OSX I need another format... does ico work for linux or do I need another format here as well? – Tom Atix Jun 01 '21 at 11:04
  • I think mac natively supports `.icns` files. Linux supports `PNG`, `XPM` and `SVG` formats more [here](https://developer.gnome.org/icon-theme-spec/). For windows, it's `.ico` file. I know it's a lot to care about while creating multi-platform applications. But it is how it is... – Just Mohit Jun 01 '21 at 13:49