How to Record your Screen in Python

Using pyautogui and OpenCV to record display screen video and save it to a file in Python.
  · 4 min read · Updated oct 2021 · Python for Multimedia

Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

Screen recording enables you to create demonstration videos, record gaming achievements, and create videos that can be shared online on social media. Many industrial software exist out there that can help you do that very easily though. In this tutorial, you will learn how to make your own simple screen recorder in Python that you can further extend to your own needs.

Related: How to Use Steganography to Hide Secret Data in Images in Python.

Let's get started, first, install the required dependencies for this tutorial:

pip3 install numpy opencv-python pyautogui

The process is as follows:

  • Capture a screenshot using pyautogui.
  • Convert that screenshot to a numpy array.
  • Write that numpy array to a file with the proper format using a video writer in OpenCV.

Importing necessary modules:

import cv2
import numpy as np
import pyautogui

Let's initialize the format we gonna use to write our video file ( named "output.avi" ):

# display screen resolution, get it from your OS settings
SCREEN_SIZE = (1920, 1080)
# define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
# create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))

Note: You need to get the correct SCREEN_SIZE from your operating system, that is the screen resolution, otherwise writing to the file won't work (alternatively, you can use pyautogui.size() function to get the size of the primary monitor).

fourcc is the video codec library that OpenCV will use to write the video file, we specified XVID here. The 20.0 float value passed as the third parameter to cv2.VideoWriter corresponds to the FPS (Frame Per Second).

Now we need to keep capturing screenshots and writing to the file in a loop until the user clicks the "q" button, here is the main loop for that:

while True:
    # make a screenshot
    img = pyautogui.screenshot()
    # convert these pixels to a proper numpy array to work with OpenCV
    frame = np.array(img)
    # convert colors from BGR to RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    # write the frame
    # show the frame
    cv2.imshow("screenshot", frame)
    # if the user clicks q, it exits
    if cv2.waitKey(1) == ord("q"):

# make sure everything is closed when exited

First, we use the screenshot() function which returns an image object, so we need to convert it to a proper numpy array. After that, we need to convert that frame into RGB, that's because OpenCV uses BGR by default.

As mentioned in the pyautogui's official documentation, you can also record only regions of your screen, by passing the region keyword argument which is a four-integer tuple representing the top, left, width, and height of the region to capture, here is how it's done:

img = pyautogui.screenshot(region=(0, 0, 300, 400))

After you are done with recording, just click "q", it will destroy the window and finish writing to the file, try it out!

Also, you can replace the while True statement with a for loop as follows:

for i in range(200):
    # make a screenshot
    img = pyautogui.screenshot()
    # the rest of the code...

This will record your screen for 10 seconds, that's because we set the FPS to 20 (which makes sense because 200 is 20 times 10)

Alright, there are endless ideas you can use to extend this. For example, you can combine this with an audio recorder, and you'll come up with a Python tool that records your screen and voice simultaneously, you will need to use a thread that records audio and another for the screen recorder, let us know your progress in the comments below!

Or you can create keyboard shortcuts that start, pause, and stops recording, this tutorial can help you.

Courses & Resources

Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more into Python, I highly suggest you get one of these amazing courses:

Learn also: How to Extract Frames from Video in Python

Happy Coding ♥

View Full Code
Sharing is caring!

Read Also

Comment panel