获取 windows 程序图标并将其保存为 .png Python
Getting a windows program icon and saving it as a .png Python
下面我有一些代码可以获取 .exe 文件并从中获取图像并将其保存为 .bmp 文件。这很好,但我需要用 .exe 文件图标具有的原始透明背景保存 .bmp。有没有办法修改下面的代码来做到这一点?
代码:
def image2(path):
path = path.replace("\", "/")
icoX = win32api.GetSystemMetrics(win32con.SM_CXICON)
icoY = win32api.GetSystemMetrics(win32con.SM_CXICON)
large, small = win32gui.ExtractIconEx(path, 0)
win32gui.DestroyIcon(small[0])
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, icoX, icoX)
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject(hbmp)
hdc.DrawIcon((0,0), large[0])
savePath = "Octo Organizer (Hybrid Edition)/Files/"
#hbmp.SaveBitmapFile(hdc, savePath + "None.bmp")
bmpinfo = dataBitMap.GetInfo()
image2("C/Users/None/Desktop/Mozilla.exe") #This is just a example file path.
最近我遇到了同样的问题。这是代码 使用 alpha-channel 保存图像 使用 PIL:
改写行
bmpinfo = dataBitMap.GetInfo()
代码:
from PIL import Image
bmpstr = hbmp.GetBitmapBits(True)
img = Image.frombuffer(
'RGBA',
(32,32),
bmpstr, 'raw', 'BGRA', 0, 1
)
img.save('icon.png')
它可以工作并使用这个 extended decision。
下面我有一些代码可以获取 .exe 文件并从中获取图像并将其保存为 .bmp 文件。这很好,但我需要用 .exe 文件图标具有的原始透明背景保存 .bmp。有没有办法修改下面的代码来做到这一点?
代码:
def image2(path):
path = path.replace("\", "/")
icoX = win32api.GetSystemMetrics(win32con.SM_CXICON)
icoY = win32api.GetSystemMetrics(win32con.SM_CXICON)
large, small = win32gui.ExtractIconEx(path, 0)
win32gui.DestroyIcon(small[0])
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, icoX, icoX)
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject(hbmp)
hdc.DrawIcon((0,0), large[0])
savePath = "Octo Organizer (Hybrid Edition)/Files/"
#hbmp.SaveBitmapFile(hdc, savePath + "None.bmp")
bmpinfo = dataBitMap.GetInfo()
image2("C/Users/None/Desktop/Mozilla.exe") #This is just a example file path.
最近我遇到了同样的问题。这是代码 使用 alpha-channel 保存图像 使用 PIL:
改写行
bmpinfo = dataBitMap.GetInfo()
代码:
from PIL import Image
bmpstr = hbmp.GetBitmapBits(True)
img = Image.frombuffer(
'RGBA',
(32,32),
bmpstr, 'raw', 'BGRA', 0, 1
)
img.save('icon.png')
它可以工作并使用这个 extended decision。