WinAPI的PIL和位图 [英] PIL and Bitmap from WinAPI

查看:88
本文介绍了WinAPI的PIL和位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码使用winapi对窗口进行截图.然后,我必须将图像保存到磁盘,然后再次将其从磁盘加载到内存PIL.是否可以立即进行任何操作而无需保存到磁盘以在PIL中传递此位图.

I have code that makes a screenshot of the window using winapi. Then I have to save image to disk and load it again from disk to memory PIL. Is there any way at once without saving to disk to pass this bitmap in the 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)

[来自另一个SO问题]

这篇关于WinAPI的PIL和位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆