How to extract 32x32 icons from raster data from EXE and convert them to a PIL image object?

I am trying to extract a 32x32 icon from an EXE and convert the raster data to a PIL Image object. My ultimate goal is to compare the icon with another 32x32 PNG and get the difference with RMS.

I tried to make win32gui.ExtractIconEx() , then win32gui.GetIconInfo() and tackle this Image.open() , but PIL does not accept PyHANDLE objects, apparently. I also tried to open exe directly with Image.open() , obviously this will not work.

I'm at a dead end right now, is this possible in Python, or should I write this part of my code in another language?

+1
source share
1 answer

From the mailing list here , I found this piece of code:

 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 can load it into PIL in the normal Image.open mode.

If you are digging documents, you should be able to avoid the I / O step and do all this in memory if you want. PIL has a frombuffer method that you can use to convert the result of GetBitmapBits to an Image object.

+3
source

All Articles