The Windows API provides the ExtractIconExW function to extract icons from executable files (.exe
) and from the icon files themselves (.ico
). Inside a .ico
file, whether it is included in an executable or not, there are often multiple images of various sizes: 16x16, 32x32, 48x48, etc. Icons that Microsoft calls "small" (usually 16x16) are generally the ones that appear in windows and the file explorer, while "large" icons (usually 32x32) are shown in the taskbar and when the user presses Alt
+ Tab
to switch between applications.
If you want to call ExtractIconExW
from Python to get the small or large icon (the function only supports these two sizes) of an executable file, the optimal solution should be pywin32, which provides a pythonic interface around the Windows API, intended to be consumed from C. Unfortunately, pywin32 does not implement the GetDIBits function, necessary to read the content (= the bitmap, each pixel) of an icon from a HICON
, which is what we get from ExtractIconExW
. Thus, we must make use of the old reliable, low-level ctypes standard module to access the Windows API.
Read more…