Skip to content

Instantly share code, notes, and snippets.

@Bailey3D
Created April 2, 2021 17:05
Show Gist options
  • Save Bailey3D/27e75739faffa9af320e1b3bc1628035 to your computer and use it in GitHub Desktop.
Save Bailey3D/27e75739faffa9af320e1b3bc1628035 to your computer and use it in GitHub Desktop.
import win32ui, win32api, win32gui, win32con
import os, sys, glob
path = "C:/Windows/explorer.exe"
def ExtractICO(exe_str, exe_name):
exe_str = exe_str.strip()
if(os.path.isfile(exe_str)):
try:
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
large, small = win32gui.ExtractIconEx(exe_str, 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] )
exe_name = exe_name.replace("[", "")
exe_name = exe_name.replace("]", "")
exe_name = exe_name.replace(" ", "_")
save_path = (os.path.dirname(sys.modules["__main__"].__file__) + "\\cache\\icons\\" + exe_name + ".bmp")
hbmp.SaveBitmapFile( hdc, save_path)
return(save_path)
except:
print("Failed to create icon for: " + exe_str)
ExtractICO(path, "abc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment