测试环境
测试情况(60张图片)
PIL: 2.038546323776245
pyautogui: 2.030569314956665
PyQt5: 2.0225892066955566
win32api: 1.0252573490142822
测试结论
win32api速度最优,几乎能实现每秒60次的屏幕获取,理论上能实现60HZ的屏幕录制。
其他几个库差别不大,速度较慢。pyautogui内部似乎是用的PIL。
测试代码
from PIL import ImageGrabimport pyautoguiimport sysfrom PyQt5.QtWidgets import QApplicationimport timeimport win32gui, win32ui, win32con, win32apidef test_win32(count=50):i = 0begin = time.time()while True:i += 1hwnd = 0hwndDC = win32gui.GetWindowDC(hwnd)mfcDC = win32ui.CreateDCFromHandle(hwndDC)saveDC = mfcDC.CreateCompatibleDC()saveBitMap = win32ui.CreateBitmap()MoniterDev = win32api.EnumDisplayMonitors(None, None)w = MoniterDev[0][2][2]h = MoniterDev[0][2][3]saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)saveDC.SelectObject(saveBitMap)saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)if i > count:breakreturn time.time() - begindef test_qt(count=50):app = QApplication(sys.argv)screen = QApplication.primaryScreen()i = 0begin = time.time()while True:i += 1screen.grabWindow(QApplication.desktop().winId())if i > count:breakreturn time.time() - begindef test_pil(count=50):i = 0begin = time.time()while True:i += 1ImageGrab.grab()if i > count:breakreturn time.time() - begindef test_auto_gui(count=50):i = 0begin = time.time()while True:i += 1pyautogui.screenshot()if i > count:breakreturn time.time() - beginprint("PIL:", test_pil(60))print("pyautogui:", test_auto_gui(60))print("PyQt5:", test_qt(60))print("win32api:", test_win32(60))
**
