【问题标题】:Python- Saving/Accessing file extension icons and using them in a Tkinter programPython-保存/访问文件扩展名图标并在 Tkinter 程序中使用它们
【发布时间】:2014-01-12 02:04:49
【问题描述】:

我目前正试图弄清楚如何创建一些代码,这些代码将输入像“.png”这样的文件扩展名并返回与系统上该文件类型关联的图标。

我正在使用 python 2.7.6 和 Windows 8。我一直在寻找这方面的代码几个小时,通过保存 .exe 文件中的图像但没有在注册表中找到文件扩展名并保存来接近它它。

我找到了一些有效的代码,允许我将文件保存为 bmp,它基本上可以通过使用 wxpython 的图标进行位图工作并保存图像来工作。但是,我希望代码根本不使用 wxpython,因为我使用 Tkinter 对接口本身进行编码。

这是来自http://ginstrom.com/scribbles/2007/08/31/file-list-with-icons-on-wxpython-windows/的当前有效代码(稍作修改)

import wx
from win32com.shell import shell, shellcon
from win32con import FILE_ATTRIBUTE_NORMAL

def extension_to_bitmap(extension):
    """dot is mandatory in extension"""

    flags = shellcon.SHGFI_SMALLICON | \
            shellcon.SHGFI_ICON | \
            shellcon.SHGFI_USEFILEATTRIBUTES

    retval, info = shell.SHGetFileInfo(extension,
                         FILE_ATTRIBUTE_NORMAL,
                         flags)
    # non-zero on success
    assert retval

    hicon, iicon, attr, display_name, type_name = info

    # Get the bitmap
    icon = wx.EmptyIcon()
    icon.SetHandle(hicon)
    return wx.BitmapFromIcon(icon)

root = wx.App()

bitmapFile = extension_to_bitmap(".png")

bitmapFile.SaveFile('test.bmp', wx.BITMAP_TYPE_BMP)

非常感谢任何帮助!

【问题讨论】:

    标签: python icons wxpython file-extension pywin32


    【解决方案1】:

    灵感来自 IronManMark20 的回答。此版本返回一个 PIL 映像,因此它不需要创建临时文件的磁盘 I/O。它还可以获取不同的图像尺寸(见下文)更多信息,请查看this博客文章。

    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
    

    【讨论】:

      【解决方案2】:

      奇怪的是,Python 似乎没有太多用于创建/编辑 *.ico 文件的功能。听起来你最好的选择是获取ImageMagick 的 Python 绑定:

      我找不到任何特定于绑定的文档。根据我的阅读,Python Imaging Library (PIL) 可以读取图标文件,但不能创建它们。但是,您可能可以使用 PIL 创建位图文件:

      【讨论】:

        【解决方案3】:

        我最近不得不做一个类似的任务,我使用了以下(需要 pywin32 和 PIL 或 Pillow)。基本上,你得到了图标的句柄,然后复制一份。这将返回一个 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
        

        【讨论】:

          猜你喜欢
          • 2011-10-10
          • 2018-05-24
          • 2021-10-27
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多