Python 获取 exe 的 icon 并且保存

参考链接:https://mail.python.org/pipermail/python-win32/2009-April/009078.html

import win32ui
import win32gui
import win32con
import win32api #ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
#ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)
ico_x = 32
ico_y = 32 exePath = "c:/windows/system32/shell32.dll" large, small = win32gui.ExtractIconEx(exePath, 0)
useIcon = large[0]
destroyIcon = small[0]
win32gui.DestroyIcon(destroyIcon) hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, ico_x, ico_x)
hdc = hdc.CreateCompatibleDC() hdc.SelectObject(hbmp)
hdc.DrawIcon(0,0), useIcon)
savePath = "d:/test.bmp"
hbmp.SaveBitmapFile(hdc, savePath)

注意:

win32gui.ExtractIconEx 只能提取32x32 和 16x16 的一大一小的图片
hbmp.SaveBitmapFile 只能在已经存在的路径下保存文件,如果路径不存在,用os.makedirs()创建

上面只是保存 bmp,如果想要保存 png,可以给 python 安装 Pillow模块,代码如下:
from PIL import Image
bmpstr = hbmp.GetBitmapBits(True)
img = Image.frombuffer(
'RGBA',
(32,32),
bmpstr, 'raw', 'BGRA', 0, 1
) img.save('icon.png')

Pillow 下载地址:https://pypi.org/project/Pillow/#files

参考链接:https://stackoverflow.com/questions/32341661/getting-a-windows-program-icon-and-saving-it-as-a-png-python

												

Python 获取 exe 的 icon 并且保存的更多相关文章

  1. Python获取exe文件版本

    import time, datetime, re, subprocess, sys, os, win32net, win32api, win32con, win32netcon, win32secu ...

  2. Python 获取 网卡 MAC 地址

    /*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: ...

  3. Python打包EXE神器 pyinstaller

    最近由于项目需要,以前的python文件需要编辑为EXE供前端客户使用. 由于最早接触的是distutils,所以一开始准备使用distutils和py2exe搭配来进行python的exe化,也就是 ...

  4. winform获取EXE图片

    winform获取EXE图片 using (FileStream fs = new System.IO.FileStream(n, FileMode.OpenOrCreate, FileAccess. ...

  5. python获取指定目录下所有文件名os.walk和os.listdir

    python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...

  6. Python获取路径下所有文件名

    python 获取当前文件夹下所有文件名   os 模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os 4 ...

  7. python获取系统信息psutil

    python获取系统信息psutil:psutil获取系统cpu使用率的方法是cpu_percent(),其有两个参数,分别是interval和percpu,interval指定的是计算cpu使用率的 ...

  8. bat wmic python 获取进程的所在路径

    bat wmic python 获取进程的所在路径 doc: wmic process where name="process-name" get executablepath w ...

  9. Pyinstaller 打包python 到exe 在windows下免python环境运行python

    在创建了独立应用(自包含该应用的依赖包)之后,还可以使用 PyInstaller 将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的 Windows 或 Mac OS X 平台上 ...

随机推荐

  1. OpenGL.英文

    1. emission 英 [iˈmɪʃn] 美 [iˈmɪʃn] 排放 n. (光.热.气等的)发出,射出,排放;排放物;散发物 material 英 [məˈtɪəriəl] 美 [məˈtɪri ...

  2. spring 机制 扫描包

    控制器示例 扫描包注解代码 @SpringBootApplication(scanBasePackages = {"cn.maxhou.*"}) 引号内为包名,支持*通配符 为什么 ...

  3. spring中的ApplicationListener监听器

    监听器在使用过程中可以监听到某一事件的发生,进而对事件做出相应的处理. 首先自定义一个监听器myListener实现ApplicationListener接口 @Repository public c ...

  4. shell中得到当下路径所有文件夹名称

      方法1: for dir in $(ls -al ./|awk '/^d/ {print $NF}') do echo $dir done   方法2: for dir in $(ls ./) d ...

  5. 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理

    目录 学习笔记:CentOS7学习之十七: Linux计划任务与日志的管理 17.1 计划任务-at-cron-计划任务使用方法 17.1.1 at计划任务的使用 17.1.2 查看和删除at将要执行 ...

  6. cs3动画

    css3 3d学习心得 卡片反转 魔方 banner图 首先我们要学习好css3 3d一定要有一定的立体感 通过这个图片应该清楚的了解到了x轴 y轴 z轴是什么概念了. 首先先给大家看一个小例子: 卡 ...

  7. (八)Spring 事务管理

    目录 文章目录 @[toc] **`Spring`** 事务管理 `Api` 介绍之 **`PlatformTransactionManager`** 后记 #Spring 的事务管理 编程式事务管理 ...

  8. vscode+php+xdebug won't stop at breakpoint 断点不起作用

    not stopping on breakpoints breakpoint not working 原因: 1) php.ini xdebug 端口不配置的情况下,默认是 9000,如果vscode ...

  9. Devexpress WinForm GridControl实现单元格可编辑状态更改

    之前做项目的时候,需要实现这样的功能.在gridcontrol中,根据是否修改(checkbox)列的选中和未选中状态来联动另外一列的编辑状态.实现如下: private void gridView1 ...

  10. java 缓存

    外存: 也就是我们经常说的(CDEF盘的大小)外储存器是指除计算机内存及CPU缓存以外的储存器,此类储存器一般断电后仍然能保存数据.常见的外存储器有硬盘.软盘.光盘.U盘等,一般的软件都是安装在外存中 ...