我有使用winapi制作窗口截图的代码。然后,我必须将图像保存到磁盘,并再次从磁盘加载到内存pil。是否有任何方法不保存到磁盘就可以在PIL中传递此位图。
import win32gui, win32ui, win32con
import Image
win_name='Book'
bmpfilenamename='1.bmp'
hWnd = win32gui.FindWindow(None, win_name)
windowcor = win32gui.GetWindowRect(hWnd)
w=windowcor[2]-windowcor[0]
h=windowcor[3]-windowcor[1]
wDC = win32gui.GetWindowDC(hWnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
#dcObj.DeleteDC()
#cDC.DeleteDC()
#win32gui.ReleaseDC(hWnd, wDC)
im=Image.open(bmpfilenamename)
im.load()
最佳答案
注释这一行:
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
并添加以下内容:
bmpinfo = dataBitMap.GetInfo()
bmpstr = dataBitMap.GetBitmapBits(True)
im = Image.frombuffer(
'RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
[从another SO question ]