如何循环pywin32截图达到每秒100张
时间: 2024-02-15 11:29:31 浏览: 97
循环pywin32截图达到每秒100张的具体代码可参考以下示例:
```python
import win32gui
import win32ui
import win32con
import numpy as np
import cv2
# 获取屏幕尺寸
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
# 创建设备描述表
hdesktop = win32gui.GetDesktopWindow()
hwndDC = win32gui.GetWindowDC(hdesktop)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
# 创建位图对象
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, screen_width, screen_height)
# 将截图保存到位图对象中
saveDC.SelectObject(saveBitMap)
saveDC.BitBlt((0, 0), (screen_width, screen_height), mfcDC, (0, 0), win32con.SRCCOPY)
# 将位图对象转换为numpy数组
signedIntsArray = saveBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (screen_height, screen_width, 4)
while True:
# 获取当前时间
start_time = cv2.getTickCount()
# 将数组转换为OpenCV BGR格式
frame = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
# 显示截图
cv2.imshow("screen", frame)
# 按'q'退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 计算每秒帧数
fps_time = cv2.getTickCount() - start_time
if fps_time == 0:
fps_time = 1
fps = cv2.getTickFrequency() / fps_time
print("FPS: ", int(fps))
# 每秒截取100张图
for i in range(100):
saveDC.BitBlt((0, 0), (screen_width, screen_height), mfcDC, (0, 0), win32con.SRCCOPY)
signedIntsArray = saveBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (screen_height, screen_width, 4)
# 释放资源
saveDC.DeleteDC()
win32gui.ReleaseDC(hdesktop, hwndDC)
cv2.destroyAllWindows()
```
阅读全文