Skip to main content

Getting the icon association for any file or folder with the Python Windows API

I wanted to make a file browser which has the correct file icons for each file type, instead of just getting them from the file name. This is a bad solution, since it has to call images from disc and may be wrong if the name is different. This method uses the Windows API to get the exact icon. Tested in Python 3.6 in Windows 10.

 from win32com.shell import shell, shellcon  
 from PIL import Image, ImageTk  
 import win32api  
 import win32con  
 import win32ui  
 import win32gui  
   
 def get_icon(PATH, size):  
   SHGFI_ICON = 0x000000100  
   SHGFI_ICONLOCATION = 0x000001000  
   if size == "small":  
     SHIL_SIZE= 0x00001  
   elif size == "large":  
     SHIL_SIZE= 0x00002  
   else:  
     raise TypeError("Invalid argument for 'size'. Must be equal to 'small' or 'large'")  
   ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_SIZE)  
   hIcon, iIcon, dwAttr, name, typeName = info  
   ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)  
   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), hIcon)  
   win32gui.DestroyIcon(hIcon)  
   
   bmpinfo = hbmp.GetInfo()  
   bmpstr = hbmp.GetBitmapBits(True)  
   img = Image.frombuffer(  
     "RGBA",  
     (bmpinfo["bmWidth"], bmpinfo["bmHeight"]),  
     bmpstr, "raw", "BGRA", 0, 1  
   )  
   
   if size == "small":  
     img = img.resize((16, 16), Image.ANTIALIAS)  
   return img  

Let's test this with a few files and folders:

The Documents folder. As you can see, it gets the two different image sizes.

The user's folder.

A standard folder.

    
It automatically gets the default application's icon. Here's VLC for an MP4 file.

Here's an example of an executable, Steam

It also works with removable media, like this SD card.

Finally, the drive with the OS installed.

I originally got the code from this SO post, but as you can see I have modified it a lot. It turns out that if the value of SHIL_SIZE is odd, then it gets the smaller image. If it's even, then it gets the larger size.

My version also returns a PIL image instead of writing to and deleting a temporary file, which uses more disc. It also returns a transparent background.

Comments

Popular posts from this blog

Using inno to package your Python application

So you have just compiled your Windows Python application. You want to make an installer for your users to easily get the application onto their PCs. You have tried using cx_Freeze's bdist_msi option but it is not advanced enough. Perhaps you want to run a script on installation or on uninstall, or want an easy way of adding an icon to the user's desktop. Now the easiest tool to use is inno. In this post we're going to take a look at how to use inno's setup wizard to package your application, as well as editing the script file. So the first thing you are going to want to do is to jump onto  inno's download page  and download inno setup compiler. Once it's done, open the application and select 'Create a new script file using the Script Wizard'. You will then be greeted with the following window: Leave the box unchecked. We're going to use the Wizard to make the base of our install script. Fill out all the information in the entry bo...

Opening Files with your Python application

For a long time now I couldn't find any help online for my problem. I want to make my own image viewer, since the one that comes with Windows 10 is so bad, but I want to be able to right click on any image, and select open with my Python application. . Lets take a look at my simple program: 1: import sys 2: 3: try: 4: print(sys.argv[1]) 5: except IndexError: 6: print("Being run without file selected.") 7: 8: input("\n\nPress any key to exit...") I want to just get a simple program working first, i'll build the application later. Normally argv would be used to parse command line arguments into a program but it turns out that when your program is compiled, when you use 'open with' your program, the second argument will be set to the path to which the file is located. The first argument is the name of the program. So the program prints the second argument. If there isn't one, if the program is just run, the...