我试图用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}$