def icon_path(exe: path_type, name: str): id_file_name = f'{name}.png' id_path = p_join(this_dir, 'icons', id_file_name) if not p_exists(id_path): ico_x = GetSystemMetrics(SM_CXICON) try: large, small = ExtractIconEx(exe, 0) except error: return default_icon_path if not len(large): return default_icon_path if len(small): DestroyIcon(small[0]) hdc = CreateDCFromHandle(GetDC(0)) h_bmp = CreateBitmap() h_bmp.CreateCompatibleBitmap(hdc, ico_x, ico_x) hdc = hdc.CreateCompatibleDC() hdc.SelectObject(h_bmp) hdc.DrawIcon((0, 0), large[0]) bmp_str = h_bmp.GetBitmapBits(True) img = Image.frombuffer('RGBA', (32, 32), bmp_str, 'raw', 'BGRA', 0, 1) img.save(id_path) print(f'Icon of {exe} saved in {id_path}') return id_path
def __getIconFromProc(self, proc): path = proc.cmdline()[0] ico_size = GetSystemMetrics(SM_CXICON) large, _ = ExtractIconEx(path, 0) hdc = CreateDCFromHandle(GetDC(0)) hbmp = CreateBitmap() hbmp.CreateCompatibleBitmap(hdc, ico_size, ico_size) hdc = hdc.CreateCompatibleDC() hdc.SelectObject(hbmp) hdc.DrawIcon((0, 0), large[0]) bmpstr = hbmp.GetBitmapBits(True) image = Image.frombuffer('RGB', (ico_size, ico_size), bmpstr, 'raw', 'BGRX', 0, 1) self.__imageList.append(image)