How to extract 32x32 icon bitmap data from EXE and convert it into a PIL Image object?

守給你的承諾、 提交于 2019-12-11 05:26:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19760913/how-to-extract-32x32-icon-bitmap-data-from-exe-and-convert-it-into-a-pil-image-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!