17

I've been trying to embed an icon (.ico) into my "compyled" .exe with py2exe.

Py2Exe does have a way to embed an icon:

windows=[{
    'script':'MyScript.py',
    'icon_resources':[(1,'MyIcon.ico')]
}]

And that's what I am using. The icon shows up fine on Windows XP or lower, but doesn't show at all on Vista. I suppose this is because of the new Vista icon format, which can be in PNG format, up to 256x256 pixels.

So, how can I get py2exe to embed them into my executable, without breaking the icons on Windows XP?

I'm cool with doing it with an external utility rather than py2exe - I've tried this command-line utility to embed it, but it always corrupts my exe and truncates its size for some reason.

nosklo
  • 217,122
  • 57
  • 293
  • 297
Etienne Perot
  • 4,764
  • 7
  • 40
  • 50

4 Answers4

20

Vista uses icons of high resolution 256x256 pixels images, they are stored using PNG-based compression. The problem is if you simply make the icon and save it in standard XP ICO format, the resulting file will be 400Kb on disk. The solution is to compress the images. The compression scheme used is PNG (Portable Network Graphic) because it has a good lossless ratio and supports alpha channel.

And use

png2ico myicon.ico logo16x16.png logo32x32.png logo255x255.png

It creates an ICO file from 1 or more PNG's and handles multiple sizes etc. And I guess XP would have no problem with that.

simplyharsh
  • 35,488
  • 12
  • 65
  • 73
  • 1
    The icon created by png2ico does work, but png2ico refuses to use the 256x256 PNG version of my icon, it only takes smaller sizes. (It says: "Width must be multiple of 8 and <256. Height must be <256.") Is there any way to go around this limitation? – Etienne Perot Feb 08 '09 at 18:07
  • Perhaps make your max resolution 255x255? I imagine it won't look much different.. – John Fouhy Feb 08 '09 at 21:21
  • 1
    I've generated a valid icon with png2ico (using a max size of 248) and specified it correctly in the py2exe setup.py, but it still won't show up as the generated exe's icon in explorer. Resource Hacker shows that the icon is embedded in the exe, but Explorer does not. This is odd. – James Nov 12 '10 at 19:55
  • As a late note to this answer: If you just added icons to your .exe file and you have a build script that keeps overwriting the same .exe file, Explorer will not show your new icon because it will have "cached" the fact that the .exe has no icon. Restarting explorer.exe (not just closing/reopening the window) fixes this. – Etienne Perot Jan 16 '12 at 06:54
  • NOTE: As @Helmut mentions below, the order MUST be reversed. Order is important. Also, I used 248x248 but not sure what's best: `png2ico.exe myico.ico myico248x248.png myico48x48.png myico32x32.png myico16x16.png` From the py2exe site: http://www.py2exe.org/index.cgi/CustomIcons – SilentSteel Aug 27 '13 at 21:30
4

I was having problems with embedding the icon resource with py2exe on Windows7 using a .ico file containing a 32x32 pixel image. I was using the same method as the original question.

Once compiled the icon on the exe disappears. On investigation the icon is added at index 0, according to the Resource Hacker tool, but if I use the same tool to replace the icon it is added at index 1. Once at index 1 the icon magically appears in explorer against the exe again.

If desperate, you could use Resource Hacker to amend the exe post-build and it can be scripted via the command line interface but I tried the method explained above and managed to get it to work after reversing the png files like so.

png2ico.exe myico.ico myico248x248.png myico48x48.png myico32x32.png myico16x16.png

By the way by adding multiple icons to the ico file you are then populating the resource at icon index 1 anyway, in this case myico248x248.png.

Richie Mackay
  • 1,385
  • 10
  • 12
  • I had the same problem. Once I wrapped multiple images into the icon file, it finally worked. – Yongwei Wu Jun 13 '17 at 10:22
  • I had this problem, but `myicon248x248.png` would still end up at resource 0. see https://stackoverflow.com/a/36116599/57461 – Pod Jan 25 '18 at 11:51
4

It seems that the order of icon sizes is the key, as said by Helmut. To invert the pages (larger ones first) solves the issue on Windows 7 for 'include_resources' (using Py2exe 0.6.9).

Gonçalo
  • 41
  • 1
3

The link to the Greenfish Iceon Editor Pro is broken. I scanned the net and found Download IcoFX Used the IcoFX program on my .exe file and could see that indeed it contained my icon.

Using the menu Image->Create Windows Icons from Image, and then accepting the choices I got a new .ico file that worked on both win7 and win xp.

Before that my single 48x48.ico file just didn't show up as an icon for the program.

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
  • Just wanted to say, IcoFX worked perfectly and supported the alpha channel of our logo. Png2ico worked, but our icon looked pixelated and wasn't good enough for a professional app. Still, Png2ico is free and IcoFX is not (tho there's a free trial and you can export with it). To make the correct icon in IcoFX, it was really easy- I just dragged and dropped it right in from a 256x256 .PNG format, clicked Save, and it made a Windows icon with all formats that worked with Py2exe. (Including the lower-res formats for older OSes.) – SilentSteel Jun 18 '14 at 01:21