如何从EXE中提取32x32图标位图数据并将其转换为PIL图像对象? [英] How to extract 32x32 icon bitmap data from EXE and convert it into a PIL Image object?

查看:458
本文介绍了如何从EXE中提取32x32图标位图数据并将其转换为PIL图像对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从EXE中提取32x32图标,并将位图数据转换为PIL Image 对象。我的最终目标是将图标与另一个32x32 PNG进行比较,并与RMS进行区分。



我尝试过 win32gui.ExtractIconEx()然后 win32gui.GetIconInfo() 并尝试使用 Image.open(),但显然PIL不接受PyHANDLE对象。我还尝试用 Image.open()直接打开exe,显然这不起作用。



<我现在很难过,有什么方法可以在Python中使用,或者我应该用不同的语言编写这部分代码?

解决方案

从邮件列表中此处,我找到了这段代码:

  import win32ui 
import win32gui
import win32con
import win32api

ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)

large,small = win32gui.ExtractIconEx (C:\Program Files(x86)\ Malwarebytes'Anti-Malware\mbam.exe,0)
win32gui.DestroyIcon(small [0])

hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(h dc,ico_x,ico_x)
hdc = hdc.CreateCompatibleDC()

hdc.SelectObject(hbmp)
hdc.DrawIcon((0,0),large [0])

hbmp.SaveBitmapFile(hdc,'icon.bmp')

来自在那里,你可以用正常的 Image.open 方式将它加载到 PIL 中。



如果你仔细阅读文档,你应该能够避免IO步骤,如果你愿意,可以在内存中完成所有操作。 PIL 有一个来自 frombuffer 的方法,可以用来转换 GetBitmapBits的结果图像对象。


I'm trying to extract a 32x32 icon from an EXE, and convert the bitmap data into an PIL Image object. My final goal is to compare the icon to another 32x32 PNG and get the difference with RMS.

I've tried doing win32gui.ExtractIconEx() then win32gui.GetIconInfo() and attemping to Image.open() on that, but PIL doesn't accept PyHANDLE objects, apparently. I've also attemped to open the exe directly with Image.open(), obviously that doesn't work.

I'm stumped right now, is there any way this is possible in Python or should I be writing this part of my code in a different language?

解决方案

From a mailing list here, I found this code snippet:

import win32ui
import win32gui
import win32con
import win32api

ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)

large, small = win32gui.ExtractIconEx("C:\Program Files (x86)\Malwarebytes' Anti-Malware\mbam.exe",0)
win32gui.DestroyIcon(small[0])

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), large[0] )

hbmp.SaveBitmapFile( hdc, 'icon.bmp')  

From there, you could load it into PIL in the normal Image.open fashion.

If you dig through the docs, you should be able to avoid the IO step and do it all in memory if you want. PIL has a from frombuffer method that you can use to convert the result of GetBitmapBits to an Image object.

这篇关于如何从EXE中提取32x32图标位图数据并将其转换为PIL图像对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆