1

How do I use python, mss, and opencv to capture my computer screen and save it as an array of images to form a movie? I am converting to gray-scale so it can be a 3 dimensional array. I would like to store each 2d screen shot in a 3d array for viewing and processing. I am having a hard time constructing an array that saves the sequence of screen shots as well as plays back the sequence of screen shots in cv2. Thanks a lot

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

2 Answers 2

2

A clue perhaps, save screenshots into a list and replay them later (you will have to adapt the sleep time):

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
Sign up to request clarification or add additional context in comments.

Comments

0

use collections.OrderedDict() to saves the sequence

import collections
....
fps_list= collections.OrderedDict()
...
fps_list[timer] = fps

1 Comment

I already saved the fps list to find the average. What I need to do is save a list of all the screenshots in the matrix_list variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.