问题
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.
回答1:
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.
回答2:
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).
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/525329/embedding-icon-in-exe-with-py2exe-visible-in-vista