Home > Software engineering >  How can I get the logo/icon of an app with path
How can I get the logo/icon of an app with path

Time:07-09

import os

path = r'C:\Users\John\AppData\Roaming\Spotify\Spotify.exe'
os.startfile(path)

What I want, in this situation for example we have Spotify, is to save the logo somewhere based on its path. I want to use it for a tkinter app I have built.

Maybe something in the idea of:

my_image = get_image_from_path(file=r'C:\Users\John\AppData\Roaming\Spotify\Spotify.exe')

CodePudding user response:

This draws an icon from a given executable into a bitmap, and saves the bitmap.

My web searching to answer this question brought up a thread on the pywin32 mailing list from 2009. I don't remember writing it, but this is the code from my reply:

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:/windows/system32/shell32.dll",0)
win32gui.DestroyIcon(large[0])

hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap( hdc, ico_x, ico_y )
hdc = hdc.CreateCompatibleDC()

hdc.SelectObject( hbmp )
hdc.DrawIcon( (0,0), small[0] )
hbmp.SaveBitmapFile( hdc, "save.bmp" )
  • Related