如何在python中从EXE中提取128x128图标位图数据

2024-10-04 03:18:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用win32gui从windows的.exe文件中提取图标。我找到了ExtractIconEx()和ExtractIcon()的功能。在

我只能从上述功能得到32x32或16x16大小的图标。下面的链接只回答了提取32x32图像的方法。 How to extract 32x32 icon bitmap data from EXE and convert it into a PIL Image object?

我需要提取大小为128x128或更大的图标那个。任何关于如何从exe文件中提取大尺寸图标的想法?在


Tags: 文件to方法图像功能链接windowsextract
2条回答

我想提取默认图标和不同的大小。基于32x32线程中Alexei的答案和Audionautics的答案,下面是代码。在

# Use wchar_t function version (FindResourceW rather than FindResourceA)
from __future__ import unicode_literals

# pywin32 imports
import win32con
import win32api
import win32file
import win32gui
import win32ui
import pywintypes

# ctypes configuring. pywin32 has no a lot of required functions
import ctypes
import ctypes.util

# memcpy used to copy data from resource storage to our buffer
libc = ctypes.CDLL(ctypes.util.find_library('c'))
libc.memcpy.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t]
libc.memcpy.restype = ctypes.c_char_p

# patch FindResourceW, ctypes.windll.kernel32.SizeofResource
FindResourceW = ctypes.windll.kernel32.FindResourceW
FindResourceW.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
FindResourceW.restype = ctypes.c_void_p
SizeofResource = ctypes.windll.kernel32.SizeofResource
SizeofResource.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
SizeofResource.restype = ctypes.c_size_t

# Using LoadLibrary (rather than CreateFile) is required otherwise 
# LoadResource, FindResource and others will fail
PATH = "C:\\Program Files\\Internet Explorer\\iexplore.exe"
hlib = win32api.LoadLibraryEx(PATH, 0, 2)

# get icon groups, default is the first group
icon_groups = win32api.EnumResourceNames(hlib, win32con.RT_GROUP_ICON)
group_name = icon_groups[0]
print group_name
hRes = win32api.LoadResource(hlib, win32con.RT_GROUP_ICON, group_name)
mem_icon_dir = ctypes.windll.kernel32.LockResource(hRes)

# 32 bits color; 16 and 256 colors are too old
# iterate through the common sizes
icon_sizes = (16, 24, 32, 48, 96, 256)
for icon_size in icon_sizes:
    icon_name = ctypes.windll.user32.LookupIconIdFromDirectoryEx(mem_icon_dir, True, icon_size, icon_size, 0x00000000);
    hResInfo = FindResourceW(hlib, icon_name, win32con.RT_ICON)
    size = ctypes.windll.kernel32.SizeofResource(hlib, hResInfo)
    rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name)
    mem_icon = ctypes.windll.kernel32.LockResource(rec)

    # And this is some differ (copy data to Python buffer)
    binary_data = (ctypes.c_ubyte * size)()
    libc.memcpy(binary_data, mem_icon, size)    
    hIconRet = ctypes.windll.user32.CreateIconFromResourceEx(binary_data, size, True, 0x00030000, 0, 0, 0x00000000);
    info = win32gui.GetIconInfo(hIconRet)
    bminfo = win32gui.GetObject(info[4])

    # generate bitmap by drawing the icon
    hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
    hbmp = win32ui.CreateBitmap()
    hbmp.CreateCompatibleBitmap(hdc, bminfo.bmWidth, bminfo.bmHeight)
    hcdc = hdc.CreateCompatibleDC()
    hcdc.SelectObject(hbmp)
    win32gui.DrawIconEx(hcdc.GetHandleOutput(), 0, 0, hIconRet, bminfo.bmWidth, bminfo.bmHeight, 0, 0, 0x0003)
    hbmp.SaveBitmapFile(hcdc, "icon-%03dx%03d-%05d-%03d.bmp" % (bminfo.bmWidth, bminfo.bmHeight, group_name, icon_name))
    win32gui.DestroyIcon(hIconRet)

我做了一些研究,也贴出来了。如果你想看到结果代码(我希望它正是你所要求的),你可以在下面的“水平规则”之后找到它。在

首先,我尝试使用下一个代码来确定文件资源中存储的图标大小:

# Using LoadLibrary (rather than CreateFile) is required otherwise 
# LoadResource, FindResource and others will fail
PATH = ...  # Valid file path
hlib = win32api.LoadLibrary(PATH)

# This loop should print sizes of resources icons
icon_names = win32api.EnumResourceNames(hlib, win32con.RT_ICON)
for icon_name in icon_names:
    rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name)
    hicon = win32gui.CreateIconFromResource(rec, True)
    info = win32gui.GetIconInfo(hicon)
    bminfo = win32gui.GetObject(info[3])
    print("%2d: 0x%08X -> %d %d " % (icon_name, hicon, bminfo.bmWidth, bminfo.bmHeight))

虽然文件只包含16x16和32x32像素的图标,但一切都会正常,下面是Windows XP calculator的输出:

^{pr2}$

一旦我尝试了使用大图标的文件,我得到了一个例外:

Traceback (most recent call last):
  File "extract_icon.py", line 50, in <module>
    hicon = win32gui.CreateIconFromResource(rec, True)
pywintypes.error: (0, 'CreateIconFromResource', 'No error message is available')

经过一些研究,我发现大图标不是以ico格式存储的,而是以png格式存储的(对于我的例子)。在

当然,我不知道你的.exe文件到底是什么(它的内部结构),但是在我分析了我在我的电脑里找到的几个.exe文件后,我发现大于32x32或16x16像素的图标最有可能是通过.png文件来表示的(你可以使用PE Explorer,试用版已经存在)。在

< >从资源中读取图像,我使用了C++上的guide。这里的主要目标是获取指向图像资源真实数据的指针,并将其复制到Python缓冲区。最后一步是把它保存到文件中(我想你可以自己把它翻译成PIL)。在


完成读取大量资源的代码

# Use wchar_t function version (FindResourceW rather than FindResourceA)
from __future__ import unicode_literals

# pywin32 imports
import pywintypes
import win32ui
import win32gui
import win32con
import win32api
import win32file

# ctypes configuring. pywin32 has no a lot of required functions
import ctypes
import ctypes.util

# memcpy used to copy data from resource storage to our buffer
libc = ctypes.CDLL(ctypes.util.find_library('c'))
libc.memcpy.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t]
libc.memcpy.restype = ctypes.c_char_p

# All Windows backslashes must be escaped to LoadLibrary worked correctly '\' -> '\\'
PATH = ...

# WARNING: Assumed that icon_name - VALID resource ID
# It can be determined in loop when enumerating resources:
# if exception at CreateIconFromResource raised than this code appropriate
# otherwise resource is standard icon and first code snippet can be used.
# If resources Id exactly known then it can be hardcoded as in this code
icon_name = 1

try:
    hlib = win32api.LoadLibrary(PATH)

    # This part almost identical to C++ 
    hResInfo = ctypes.windll.kernel32.FindResourceW(hlib, icon_name, win32con.RT_ICON)
    size = ctypes.windll.kernel32.SizeofResource(hlib, hResInfo)
    rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name)
    mem_pointer = ctypes.windll.kernel32.LockResource(rec)

    # And this is some differ (copy data to Python buffer)
    binary_data = (ctypes.c_ubyte * size)()
    libc.memcpy(binary_data, mem_pointer, size)

    # Save it
    with open("icon.png", "wb") as test_file:
        test_file.write(bytearray(binary_data))

except pywintypes.error as error:
    print "ERROR: %s" % error.strerror
    raise

更新

自动查找非图标资源并将其提取到名为“Resource_XX”的文件的代码:

# Same IMPORT's as previously should be used

# All Windows backslashes must be escaped to LoadLibrary worked correctly '\' -> '\\'
PATH = ...


def extract(rec):
    try:
        hicon = win32gui.CreateIconFromResource(rec, True)
    except pywintypes.error as error:
        # Check on appropriate error
        if error.winerror != 6:
            raise

        print("Resource %2d isn't .ico, extract" % icon_name)
        # This part almost identical to C++ 
        hResInfo = ctypes.windll.kernel32.FindResourceW(hlib, icon_name, win32con.RT_ICON)
        size = ctypes.windll.kernel32.SizeofResource(hlib, hResInfo)
        mem_pointer = ctypes.windll.kernel32.LockResource(rec)

        # And this is some differ (copy data to Python buffer)
        binary_data = (ctypes.c_ubyte * size)()
        libc.memcpy(binary_data, mem_pointer, size)

        # Save it
        with open("Resource_%s.png" % icon_name, "wb") as extract_file:
            extract_file.write(bytearray(binary_data))
    else:
        info = win32gui.GetIconInfo(hicon)
        bminfo = win32gui.GetObject(info[3])
        print("Resource %2d is .ico: 0x%08X -> %d %d " % 
                  (icon_name, hicon, bminfo.bmWidth, bminfo.bmHeight))


try:
    hlib = win32api.LoadLibrary(PATH)
    icon_names = win32api.EnumResourceNames(hlib, win32con.RT_ICON)
    for icon_name in icon_names:
        rec = win32api.LoadResource(hlib, win32con.RT_ICON, icon_name)
        extract(rec)

except pywintypes.error as error:
    print "ERROR: %s" % error.strerror
    raise

相关问题 更多 >