python读取内存位图_用Pywin32裁剪内存中的位图图像

我试图用pywin32在多个监视器上截图。在

我有第三台显示器上需要的屏幕截图,但我只需要图像的某个区域。在

我可以在将位图图像保存到硬盘驱动器时进行裁剪,如下所示:# initial set up code hooking up pywin32 to the desktop

import win32gui, win32ui, win32con, win32api

hwin = win32gui.GetDesktopWindow()

# example dimensions

width = 800

height = 600

left = 3300

top = 100

hwindc = win32gui.GetWindowDC(hwin)

srcdc = win32ui.CreateDCFromHandle(hwindc)

memdc = srcdc.CreateCompatibleDC()

bmp = win32ui.CreateBitmap()

bmp.CreateCompatibleBitmap(srcdc, width, height)

memdc.SelectObject(bmp)

# saving of the file (what I am currently doing)

bmp.SaveBitmapFile(memdc, 'fullshot.bmp')

# strangely enough this crops the portion I need,

# but within an image that's the entire length of my desktop

# (not sure how to fix that, you could say this is part of the problem)

memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)

img = Image.open('fullshot.bmp')

img = img.crop((0,0,800,600))

# now the cropped image is in memory but I want just the portion I need without saving it to disk

bmp的类型为“PyCBitmap”。我试过np.array(bmp),但这也没用。有没有办法把pwin32截取的bmp截图裁剪成程序内存中需要的尺寸?在

更新:

我尝试了以下代码,但也不起作用。当我试图用cv2.imshow('image', img)显示它时,我得到一个没有响应的窗口。在

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值