-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiconProvider.py
More file actions
60 lines (46 loc) · 1.68 KB
/
iconProvider.py
File metadata and controls
60 lines (46 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from win32com.shell import shell, shellcon # pycharm cannot find those import but it will be interpreted
from PIL import Image
import win32api
import win32con
import win32ui
import win32gui
# Import the core and GUI elements of Qt
from PyQt5.QtGui import *
def get_icon(PATH, size):
SHGFI_ICON = 0x000000100
SHGFI_ICONLOCATION = 0x000001000
SHGFI_USEFILEATTRIBUTES = 0x000000010
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 | SHGFI_USEFILEATTRIBUTES)
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)
data = img.tobytes("raw", "RGBA")
qim = QImage(data, img.size[0], img.size[1], QImage.Format_RGBA8888)
# qim.save("test.png")
return qim
def main():
img = get_icon("test.png", "small")
img.save("test.png", "png")
if __name__ == "__main__":
main()