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.

4 Answers
4

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.

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 PerotFeb 8 '09 at 18:07

Perhaps make your max resolution 255x255? I imagine it won't look much different..
–
John FouhyFeb 8 '09 at 21:21

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.
–
JamesNov 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 PerotJan 16 '12 at 6: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: py2exe.org/index.cgi/CustomIcons
–
SilentSteelAug 27 '13 at 21:30

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.)
–
SilentSteelJun 18 '14 at 1:21

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.