1

如何使用 python、mss 和 opencv 捕获我的计算机屏幕并将其保存为图像数组以形成电影?我正在转换为灰度,因此它可以是 3 维数组。我想将每个 2d 屏幕截图存储在 3d 数组中以供查看和处理。我很难构建一个数组来保存屏幕截图序列以及回放 cv2 中的屏幕截图序列。非常感谢

import time
import numpy as np
import cv2
import mss
from PIL import Image

with mss.mss() as sct:
    fps_list=[]
    matrix_list = []
    monitor = {'top':40, 'left':0, 'width':800, 'height':640}
    timer = 0
    while timer <100:
        last_time = time.time()

        #get raw pizels from screen and save to numpy array
        img = np.array(sct.grab(monitor))
        img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        #Save img data as matrix
        matrix_list[timer,:,:] = img

        #Display Image
        cv2.imshow('Normal', img)
        fps = 1/ (time.time()-last_time)
        fps_list.append(fps)

        #press q to quit
        timer += 1
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
#calculate fps
fps_list = np.asarray(fps_list)
print(np.average(fps_list))


#playback image movie from screencapture
t=0
while t < 100:
    cv.imshow('Playback',img_matrix[t])
    t += 1
4

2 回答 2

1

也许是一个线索,将屏幕截图保存到列表中并稍后重播(您必须调整睡眠时间):

import time
import cv2
import mss
import numpy


with mss.mss() as sct:
    monitor = {'top': 40, 'left': 0, 'width': 800, 'height': 640}
    img_matrix = []

    for _ in range(100):
        # Get raw pizels from screen and save to numpy array
        img = numpy.array(sct.grab(monitor))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # Save img data as matrix
        img_matrix.append(img)

        # Display Image
        cv2.imshow('Normal', img)

        # Press q to quit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

    # Playback image movie from screencapture
    for img in img_matrix:
        cv2.imshow('Playback', img)

        # Press q to quit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
于 2017-07-30T09:16:13.427 回答
0

用于collections.OrderedDict()保存序列

import collections
....
fps_list= collections.OrderedDict()
...
fps_list[timer] = fps
于 2017-07-27T03:21:11.323 回答