Skip to content

Instantly share code, notes, and snippets.

@chyyran
Created November 5, 2013 06:14
Show Gist options
  • Save chyyran/7314682 to your computer and use it in GitHub Desktop.
Save chyyran/7314682 to your computer and use it in GitHub Desktop.
Gets the 16x16 icon from an EXE and returns a PIL.Image object
def get_icon(exe):
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
ico_y = win32api.GetSystemMetrics(win32con.SM_CYICON)
large, small = win32gui.ExtractIconEx(exe, 0)
if len(small) == 0:
return False
win32gui.DestroyIcon(large[0])
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
icon_bmp = win32ui.CreateBitmap()
icon_bmp.CreateCompatibleBitmap(hdc, ico_x, ico_y)
hdc = hdc.CreateCompatibleDC()
hdc.SelectObject(icon_bmp)
hdc.DrawIcon((0,0), small[0]) #draw the icon before getting bits
icon_info = icon_bmp.GetInfo()
icon_buffer = icon_bmp.GetBitmapBits(True)
icon = Image.frombuffer('RGB', (icon_info['bmWidth'], icon_info['bmHeight']), icon_buffer, 'raw', 'BGRX', 0, 1)
win32gui.DestroyIcon(small[0])
return icon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment