使用16位图像数据的Image.frombuffer

0 投票
1 回答
5024 浏览
提问于 2025-04-16 07:10

如果我的Windows系统是32位颜色深度模式,那么下面的代码可以从一个窗口获取到一个不错的PIL图像:

def image_grab_native(window):
    hwnd = win32gui.GetDesktopWindow()

    left, top, right, bot = get_rect(window)
    w = right - left
    h = bot - top

    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()

    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h),  mfcDC,  (left, top),  win32con.SRCCOPY)

    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)

    im = Image.frombuffer(
        'RGB',
        (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
        bmpstr, 'raw', 'BGRX', 0, 1)

    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)

    return im

但是,当我在16位模式下运行时,就会出现错误:

>>> image_grab_native(win)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    image_grab_native(win)
  File "C:\claudiu\bumhunter\finderbot\ezpoker\utils\win32.py", line 204, in image_grab_native
    bmpstr, 'raw', 'BGRX', 0, 1)
  File "c:\python25\lib\site-packages\PIL\Image.py", line 1808, in frombuffer
    return apply(fromstring, (mode, size, data, decoder_name, args))
  File "c:\python25\lib\site-packages\PIL\Image.py", line 1747, in fromstring
    im.fromstring(data, decoder_name, args)
  File "c:\python25\lib\site-packages\PIL\Image.py", line 575, in fromstring
    raise ValueError("not enough image data")
ValueError: not enough image data

我该如何构造frombuffer的调用,以便在16位模式下正常工作?另外,我怎么才能让这个函数在任何位深度模式下都能工作,而不是每次都得传一个参数呢?

更新:从这个问题中我了解到,我必须使用"BGR;16"而不是"BGRX"作为第二个模式参数。这样可以正确获取图像,无论是否指定步幅。问题是某些像素值稍微有点偏差:

x   y native           ImageGrab
280 0  (213, 210, 205) (214, 211, 206)
280 20 (156, 153, 156) (156, 154, 156)
280 40 (213, 210, 205) (214, 211, 206)
300 0  (213, 210, 205) (214, 211, 206)

这是从同一个窗口中取的一些值样本。截图在肉眼看起来是一样的,但我需要对一些像素进行处理……另外,我想使用本地方法的原因是它稍微快一点,而且在双显示器的虚拟机中运行时表现更好……(是的,听起来有点复杂,我知道)。

1 个回答

2

关于 stride 参数,你需要提供每行的字节大小。你的每个像素是16位的,所以你可能会简单地认为 stride = 2*bmpinfo['bmWidth']; 不过,Windows会添加一些填充,使得这个值变成32位的整数倍。这意味着你需要把它调整到下一个4的倍数:stride = (stride + 3) / 4) * 4

文档里没有提到16位的原始格式,所以你需要查看 Unpack.c 模块,看看有哪些可用的格式。

最后你会发现,Windows喜欢把它的位图倒过来。

补充:你最后遇到的小问题很容易解释——从16位转换到24位的过程并没有一个明确的标准,因此在两种不同的转换之间出现一个单位的差异是很正常的。在你转换完数据后,调整这些差异并不难,因为我相信这些差异是基于值的常量。

撰写回答