python 使用 py2exe 在 .exe 中嵌入图标,在 Vista 中可见?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/525329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:17:35  来源:igfitidea点击:

Embedding icon in .exe with py2exe, visible in Vista?

pythonwindows-vistaembediconspy2exe

提问by Etienne Perot

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

我一直在尝试使用 py2exe 将图标 (.ico) 嵌入到我的“编译”.exe 中。

Py2Exe does have a way to embed an icon:

Py2Exe 确实有一种嵌入图标的方法:

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.

这就是我正在使用的。该图标在 Windows XP 或更低版本上显示良好,但在 Vista 上根本不显示。我想这是因为新的 Vista 图标格式,可以是 PNG 格式,最大为 256x256 像素。

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

那么,如何让 py2exe 将它们嵌入到我的可执行文件中,而不会破坏 Windows XP 上的图标?

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

我很高兴使用外部实用程序而不是 py2exe 来执行此操作 - 我已尝试使用此命令行实用程序来嵌入它,但它总是会损坏我的 exe 并出于某种原因截断其大小。

回答by simplyharsh

Vista uses icons of high resolution 256x256pixels images, they are stored using PNG-basedcompression. The problem is if you simply make the icon and save it in standard XP ICOformat, the resulting file will be 400Kbon 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.

Vista 使用高分辨率256x256像素图像的图标,它们使用基于 PNG 的压缩进行存储。问题是,如果您只是简单地制作图标并将其保存为标准 XPICO格式,则生成的文件将400Kb在磁盘上。解决方案是压缩图像。使用的压缩方案是PNG(Portable Network Graphic),因为它具有良好的无损比率并支持 alpha 通道。

And use

并使用

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

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

ICO从 1 个或多个创建一个文件PNG并处理多个大小等。我想 XP 不会有问题。

回答by Richie Mackay

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.

我在使用包含 32x32 像素图像的 .ico 文件在 Windows7 上使用 py2exe 嵌入图标资源时遇到问题。我使用的方法与原始问题相同。

Once compiled the icon on the exe disappears. On investigation the icon is added at index 0, according to the Resource Hackertool, 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.

一旦编译,exe 上的图标就会消失。在调查中,根据Resource Hacker工具,图标添加在索引 0 处,但如果我使用相同的工具替换图标,它会添加在索引 1 处。一旦在索引 1 处,图标再次神奇地出现在资源管理器中,与 exe 相对。

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.

如果不顾一切,您可以使用 Resource Hacker 来修改 exe post-build,它可以通过命令行界面编写脚本,但我尝试了上面解释的方法,并在像这样反转 png 文件后设法让它工作。

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.

顺便说一下,通过向 ico 文件添加多个图标,您无论如何都会在图标索引 1 处填充资源,在本例中为 myico248x248.png。

回答by Gon?alo

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).

正如 Helmut 所说,图标大小的顺序似乎是关键。反转页面(首先是较大的页面)解决了 Windows 7 上“include_resources”(使用 Py2exe 0.6.9)的问题。

回答by Anders Hedberg

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

Greenfish Iceon Editor Pro 的链接已损坏。我扫描了网络,发现下载 IcoFX在我的 .exe 文件中使用了 IcoFX 程序,可以看到它确实包含我的图标。

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.

使用菜单 Image->Create Windows Icons from Image,然后接受这些选择,我得到了一个新的 .ico 文件,它可以在 win7 和 win xp 上运行。

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

在此之前,我的单个 48x48.ico 文件没有显示为程序的图标。