小编SHR*_*ISH的帖子

使用 PIL 或 cv2 等模块在 python 中捕获屏幕的最有效方法是什么?因为它占用了很多内存

使用 PIL 或 cv2 等模块在 python 中捕获屏幕的最有效方法是什么?因为它占用大量内存。

我想教 AI 通过屏幕抓取和整洁的方式来玩 Chrome 的恐龙游戏,但它的速度很慢......

我努力了:

import numpy as np
from PIL import ImageGrab
import cv2
import time

last_time = time.time()
while True:
    printscreen_pil = ImageGrab.grab(bbox= (0, 40, 800, 640))

    printscreen_numpy = np.array(printscreen_pil.getdata(), dtype = 'uint8').reshape((printscreen_pil.size[1], printscreen_pil.size[0], 3))
    
    print(f'the loop took {time.time() - last_time} seconds')
    last_time = time.time()
 
     cv2.imshow('window', printscreen_numpy)
     if cv2.waitKey(25) & 0xFF == ord('q'):
         cv2.destroyAllWindows()
         break

> 
    # average time = the loop took 2.068769931793213 seconds 
Run Code Online (Sandbox Code Playgroud)

python performance opencv screen-scraping screen

4
推荐指数
1
解决办法
3352
查看次数

标签 统计

opencv ×1

performance ×1

python ×1

screen ×1

screen-scraping ×1