第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 PIL 或 cv2 等模塊在 python 中捕獲屏幕的最有效方法是什么?

使用 PIL 或 cv2 等模塊在 python 中捕獲屏幕的最有效方法是什么?

動漫人物 2023-07-11 14:36:34
使用 PIL 或 cv2 等模塊在 python 中捕獲屏幕的最有效方法是什么?因為它占用大量內(nèi)存。我想教 AI 通過屏幕抓取和整潔的方式來玩 Chrome 的恐龍游戲,但它的速度很慢......我努力了:import numpy as npfrom PIL import ImageGrabimport cv2import timelast_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 
查看完整描述

1 回答

?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗 獲得超12個贊

您可以使用mss“純 python 中的超快速跨平臺多個屏幕截圖模塊”。

例如:

import time

import cv2

import numpy as np

from mss import mss


start_time = time.time()

mon = {'top': 200, 'left': 200, 'width': 200, 'height': 200}

with mss() as sct:

? ? while True:

? ? ? ? last_time = time.time()

? ? ? ? img = sct.grab(mon)

? ? ? ? print('The loop took: {0}'.format(time.time()-last_time))

? ? ? ? cv2.imshow('test', np.array(img))

? ? ? ? if cv2.waitKey(25) & 0xFF == ord('q'):

? ? ? ? ? ? cv2.destroyAllWindows()

? ? ? ? ? ? break

結(jié)果:


The loop took: 0.024120092391967773

輸出:

http://img4.sycdn.imooc.com/64acf8a4000104fb04020405.jpg

結(jié)果比當(dāng)前結(jié)果快 100 倍。



查看完整回答
反對 回復(fù) 2023-07-11
  • 1 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號