Python-保存/访问文件扩展名图标并在Tkinter程序中使用它们
我目前正试图找出如何创建一些代码,这些代码将接受文件扩展名(如“.png”)的输入,并在系统上返回与该文件类型关联的图标 我正在使用Python2.7.6和Windows8。我已经找了好几个小时的代码,通过保存.exe文件中的图像,但在注册表中找不到文件扩展名并保存它,来实现类似的功能 我找到了一些代码,可以将文件保存为bmp,基本上是通过使用wxpython的图标对工作进行位图处理,并保存图像。但是,我希望代码不要使用wxpython,因为我使用Tkinter来编写接口本身 下面是当前从Python-保存/访问文件扩展名图标并在Tkinter程序中使用它们,python,icons,wxpython,file-extension,pywin32,Python,Icons,Wxpython,File Extension,Pywin32,我目前正试图找出如何创建一些代码,这些代码将接受文件扩展名(如“.png”)的输入,并在系统上返回与该文件类型关联的图标 我正在使用Python2.7.6和Windows8。我已经找了好几个小时的代码,通过保存.exe文件中的图像,但在注册表中找不到文件扩展名并保存它,来实现类似的功能 我找到了一些代码,可以将文件保存为bmp,基本上是通过使用wxpython的图标对工作进行位图处理,并保存图像。但是,我希望代码不要使用wxpython,因为我使用Tkinter来编写接口本身 下面是当前从 非常
非常感谢您的帮助 奇怪的是,Python似乎没有太多用于创建/编辑*.ico文件的功能。听起来您最好的选择是为以下内容获取Python绑定:
Icontemp.bmp
def icon32(PATH):
SHGFI_ICON = 0x000000100
SHGFI_ICONLOCATION = 0x000001000
SHIL_EXTRALARGE = 0x00002
ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_EXTRALARGE)
hIcon, iIcon, dwAttr, name, typeName = info
tempDirectory = os.getenv("temp")
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
#creating a destination memory DC
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)
hbmp.SaveBitmapFile(hdc, tempDirectory + "\Icontemp.bmp")
icon = QIcon(tempDirectory + "\Icontemp.bmp")
os.remove(tempDirectory + "\Icontemp.bmp")
return icon
我最近不得不做一个类似的任务,我使用了以下方法(需要pywin32和PIL或枕头)。基本上,你得到了图标的句柄,并复制了一个。这将返回一个PIL图像。如果需要,可以使用其他工具打开
Icontemp.bmp
def icon32(PATH):
SHGFI_ICON = 0x000000100
SHGFI_ICONLOCATION = 0x000001000
SHIL_EXTRALARGE = 0x00002
ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_EXTRALARGE)
hIcon, iIcon, dwAttr, name, typeName = info
tempDirectory = os.getenv("temp")
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
#creating a destination memory DC
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)
hbmp.SaveBitmapFile(hdc, tempDirectory + "\Icontemp.bmp")
icon = QIcon(tempDirectory + "\Icontemp.bmp")
os.remove(tempDirectory + "\Icontemp.bmp")
return icon
我最近不得不做一个类似的任务,我使用了以下方法(需要pywin32和PIL或枕头)。基本上,你得到了图标的句柄,并复制了一个。这将返回一个PIL图像。如果需要,可以使用其他工具打开
Icontemp.bmp
def icon32(PATH):
SHGFI_ICON = 0x000000100
SHGFI_ICONLOCATION = 0x000001000
SHIL_EXTRALARGE = 0x00002
ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_EXTRALARGE)
hIcon, iIcon, dwAttr, name, typeName = info
tempDirectory = os.getenv("temp")
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
#creating a destination memory DC
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)
hbmp.SaveBitmapFile(hdc, tempDirectory + "\Icontemp.bmp")
icon = QIcon(tempDirectory + "\Icontemp.bmp")
os.remove(tempDirectory + "\Icontemp.bmp")
return icon
我最近不得不做一个类似的任务,我使用了以下方法(需要pywin32和PIL或枕头)。基本上,你得到了图标的句柄,并复制了一个。这将返回一个PIL图像。如果需要,可以使用其他工具打开
Icontemp.bmp
def icon32(PATH):
SHGFI_ICON = 0x000000100
SHGFI_ICONLOCATION = 0x000001000
SHIL_EXTRALARGE = 0x00002
ret, info = shell.SHGetFileInfo(PATH, 0, SHGFI_ICONLOCATION | SHGFI_ICON | SHIL_EXTRALARGE)
hIcon, iIcon, dwAttr, name, typeName = info
tempDirectory = os.getenv("temp")
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)
#creating a destination memory DC
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)
hbmp.SaveBitmapFile(hdc, tempDirectory + "\Icontemp.bmp")
icon = QIcon(tempDirectory + "\Icontemp.bmp")
os.remove(tempDirectory + "\Icontemp.bmp")
return icon
灵感来自IronManMark20的答案。此版本返回一个PIL映像,因此不需要磁盘I/O来创建临时文件。它还可以得到不同的图像大小(见下文)更多信息,请查看博客文章
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
灵感来自IronManMark20的答案。此版本返回一个PIL映像,因此不需要磁盘I/O来创建临时文件。它还可以得到不同的图像大小(见下文)更多信息,请查看博客文章
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
灵感来自IronManMark20的答案。此版本返回一个PIL映像,因此不需要磁盘I/O来创建临时文件。它还可以得到不同的图像大小(见下文)更多信息,请查看博客文章
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
灵感来自IronManMark20的答案。此版本返回一个PIL映像,因此不需要磁盘I/O来创建临时文件。它还可以得到不同的图像大小(见下文)更多信息,请查看博客文章
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