2010-11-16 44 views
0

如果我的窗口是在32位颜色深度模式,然后将以下代码得到一个很好的PIL图像从一个窗口:Image.frombuffer具有16位的图像数据

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位模式下工作?另外我怎样才能使这个功能在任何位深度模式下工作,而不是说必须将它作为参数传递?

更新:从this question我学会了第二模式参数必须使用“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) 

只是取自同一窗口的值样本。截图看起来与肉眼完全一样,但我必须做一些像素操作..也是我想要使用本地方法的原因是它速度更快,并且在双显示器内运行虚拟机时表现更好。 (是的,我认识的是非常随机的复杂)。

回答

2

对于stride参数,您需要以字节为单位给出行大小。你的像素是每个16位,所以你可能天真地假设stride = 2*bmpinfo['bmWidth'];不幸的是Windows增加了填充以使跨度达到32位的倍数。这意味着您必须将其舍入到4的下一个最高倍数:stride = (stride + 3)/4) * 4

documentation没有提及16位原始格式,因此您必须检查Unpack.c模块以查看可用内容。

你会注意到的最后一件事是Windows喜欢颠倒它的位图。

编辑:您最后的小问题很容易解释 - 从16位到24位的转换没有精确定义,两个不同转换之间的偏差是完全正常的。在转换数据后调整数据并不难,因为我确定基于该值的差异是不变的。

+0

我很密集,但哪一个是步幅参数?那是我目前作为一个元组? – Claudiu 2010-11-16 22:39:41

+3

'bmpstr,'raw','BGRX',0,1)'< - 该行为0 – 2010-11-16 22:41:12

+0

hmm小新问题,请参阅更新 – Claudiu 2010-11-16 23:10:43