Opencv python screen capture

Screen Recording using python

The next step is to specify video codec using fourCC . It is a 4 byte code used for specifying the video codec. For the sake of simplicity we can pass the FourCC code for AVI video format which is *»XVID»

codec = cv2.VideoWriter_fourcc(*"XVID") 

In the next step, we will be creating a videoWriter object.
The video writer method of cv2 module needs 4 mandatory arguments.
First being the filename , second being the codec , third being the FPS (Frame per second) and the last being the video (or Screen) resolution. For a better screen recording 60 FPS is a good frame rate. You can experiment around the FPS value to get the optimum results.

out = cv2.VideoWriter("Recorded.avi", codec , 60, (1366, 768)) #Here screen resolution is 1366 x 768, you can change it depending upon your need 

The next step is to implicitly create a window using namedWindow() method with the WINDOW_NORMAL flag so that the user can resize the window depending upon the need.

cv2.namedWindow("Recording", cv2.WINDOW_NORMAL) cv2.resizeWindow("Recording", 480, 270) #Here we are resizing the window to 480x270 so that the program doesn't run in full screen in the beginning 

Now we need to capture screenshot of each frame of the screen, write it to the video file and display the screen being recorded.
Here is the code snippet:

while True: img = pyautogui.screenshot() #capturing screenshot frame = np.array(img) #converting the image into numpy array representation frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #converting the BGR image into RGB image out.write(frame) #writing the RBG image to file cv2.imshow('Recording', frame) #display screen/frame being recorded if cv2.waitKey(1) == ord('q'): #Wait for the user to press 'q' key to stop the recording break out.release() #closing the video file cv2.destroyAllWindows() #destroying the recording window 

Notice that the screenshot captured is in BGR format but in order to write the frame to the video file we need to convert it into RGB format using cvt.Color() method with COLOR_BGR2RBG flag. The condition if cv2.waitKey(1) == ord(‘q’) waits for the user to press ‘q’ key to quit the recording. Lastly we have closed the video file and destroyed the recording window by release() and destroyAllWindows() method respectively.
PS. Linux users please don’t be happy after hearing «Destroy all Windows» 😉

Читайте также:  Поле с паролем

You are all done!
It’s time to test the results.

Alt Text

Maximized window: (Horrible infinite loop)

Источник

How to record your screen using Python 🐍

How to record your screen using Python

Before getting into it, one should first understand what actually a video is. Basically, a video is nothing but a sequence of images (called frames) displayed at a given frequency.

So a workaround to make a screen recorder would be to capture each frame of the screen and write it as a video file.

To do this python comes with handy libraries. For the screen recording, we will be using OpenCV, PyAutoGUI, and Numpy.

Read more about OpenCV in our last article.

OpenCV- An awesome tool for computer vision in python

#python #machinelearning #open-cv

October 31, 2020 3 mins read

Make sure you have all the above mentioned necessary libraries installed.

  1. OpenCV: To install OpenCV, run pip install opencv-python
  2. PyAutoGui: To install PyAutoGui, run pip install pyautogui
  3. Numpy: To install Numpy, run pip install numpy

The first step is to import all the modules needed.

import cv2 #OpenCV import pyautogui import numpy as np 

The next step is to specify the video codec using fourCC . It is a 4-byte code used for specifying the video codec. For the sake of simplicity we can pass the FourCC code for AVI video format which is *»XVID» .

codec = cv2.VideoWriter_fourcc(*"XVID") 

In the next step, we will be creating a videoWriter object. The video writer method of cv2 module needs 4 mandatory arguments.

First is the filename , second being the codec , third being the FPS (Frame per second) and the last being the video (or Screen) resolution.

For a better screen recording, 60 FPS is a good frame rate. You can experiment around the FPS value to get the optimum results.

out = cv2.VideoWriter("Recorded.avi", codec , 60, (1366, 768)) #Here screen resolution is 1366 x 768, you can change it depending upon your need 

The next step is to implicitly create a window using namedWindow() method with the WINDOW_NORMAL flag so that the user can resize the window depending upon the need.

cv2.namedWindow("Recording", cv2.WINDOW_NORMAL) cv2.resizeWindow("Recording", 480, 270) #Here we are resizing the window to 480x270 so that the program doesn't run in full screen in the beginning 

Now we need to capture a screenshot of each frame of the screen, write it to the video file and display the screen being recorded.

while True: img = pyautogui.screenshot() #capturing screenshot frame = np.array(img) #converting the image into numpy array representation frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #converting the BGR image into RGB image out.write(frame) #writing the RBG image to file cv2.imshow('Recording', frame) #display screen/frame being recorded if cv2.waitKey(1) == ord('q'): #Wait for the user to press 'q' key to stop the recording break out.release() #closing the video file cv2.destroyAllWindows() #destroying the recording window 

Notice that the screenshot captured is in BGR format but in order to write the frame to the video file, we need to convert it into RGB format using cvt.Color() method with COLOR_BGR2RBG flag.

Читайте также:  Текст

The condition if cv2.waitKey(1) == ord(‘q’) waits for the user to press ‘q’ key to quit the recording.

Lastly, we have closed the video file and destroyed the recording window by release() and destroyAllWindows() method respectively.

PS: Linux users please don’t be happy after hearing “Destroy all Windows”😂

It’s time to test the results.

Normal Sized window

Alt Text

Maximized window: (Horrible infinite loop)

I was making a screen recorder in #python using #opencv and it gave me some weird output😳

Источник

How to Make a Screen Recorder in Python?

How to Make a Screen Recorder in Python?

There are many screen recording applications for different operating systems. With the help of these screen recording applications, we can record the screen while we are playing a video game, writing some code, researching something on the internet, and so on.

In this Python tutorial, we will discuss how to make a screen recorder in Python. Although Windows and macOS now come with a built-in screen recorder application, it would be cool if you knew how to build a screen recorder of your own using Python.

But before we dive into the Python implementation of the screen recorder, let’s install the libraries we will be using in this tutorial.

Installing Libraries

1) The pyautogui Library

pyautogui is a Python open-source third-party API that is used to control the mouse, keyboard, and other automated interactions. In this tutorial, we will be using this API to take screenshots and then using other libraries to chain those screen shows and create a screen recorder. To install pyautogui for your Python environment, run the following pip install command:

2) The opencv-python Library

OpenCV is a Python computer vision library that is used to handle images and video files. In this tutorial, we will be using this library to write video data. As a video is a collection of images, so here we will use the OpenCV library to write all the screenshots captured by the pyautogui API. Use the following pip command to install the OpenCV library:

3) The numpy Library

NumPy is very popular among Python developers for its numerical computation and NumPy arrays. When you install opencv-python , it will automatically install numpy. To install the numpy library, use the following pip install command:

Now that we are all set with installing all the required libraries, let’s write the Python code.

How to Make a Screen Recorder in Python?

Let’s start with importing the required modules.

import cv2 as cv import pyautogui import numpy as np

Now, let’s get the screen size of our display using the pyautogui. size() function.

#(width,height) screen_size=pyautogui.size()

Next, we need to initialize the VideoWriter() object that will write the video frames.

#initialize the object video = cv.VideoWriter('Recording.avi', cv.VideoWriter_fourcc(*'MJPG'), 20, screen_size)
  • Recording.avi is the file name of the video that we will be recording.
  • cv.VideoWriter_fourcc(*’MJPG’) will set the four-character code that compresses the frames.
  • 20 is the framerate of videostream.
  • screen_size is the height and width of the video frame.
Читайте также:  Template design using html

Now, we need to create a loop that will capture the screenshot of the display and write those images in the video object.

print("Recording. ") while True: #take screenshot screen_shot_img = pyautogui.screenshot() #convert into array frame = np.array(screen_shot_img) #change from BGR to RGB frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB) #write frame video.write(frame) #display the live recording cv.imshow("Recording Frame(Minimize it)", frame) if cv.waitKey(1) == ord("q"): break cv.destroyAllWindows() video.release()
  • The screenshot() function will capture the screen.
  • array(screen_shot_img) will convert the screen_shot_image into a numpy array because OpenCV is supposed to work with numpy arrays.
  • cvtColor() will change the color format of the image from BGR to RGB because OpenCV, by default, writes images in BRG format, so it’s important to convert them into RGB.
  • The write(frame) function will write the frame into the video object.
  • imshow() will display a live video recording. For better performance, you can minimize the recording frame window.
  • To close the screen recording, either press «q» on the live recording screen or kill the program by typing CTRL+Z.

Now put all the code together and execute.

Python Program to Make a Screen Recorder

import cv2 as cv import pyautogui import numpy as np #(width,height) screen_size=pyautogui.size() #initialize the object video = cv.VideoWriter('Recording.avi', cv.VideoWriter_fourcc(*'MJPG'), 20, screen_size) print("Recording. ") while True: #click screen shot screen_shot_img = pyautogui.screenshot() #convert into array frame = np.array(screen_shot_img) #change from BGR to RGB frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB) #write frame video.write(frame) #display the live recording cv.imshow("Recording Frame(Minimize it)", frame) if cv.waitKey(1) == ord("q"): break cv.destroyAllWindows() video.release()

After successfully executing and recording the screen, you can check the directory where your Python script is located. There you will find the Recording.avi video of your screen recording.

Conclusion

In this Python tutorial, we learned how we could code for a simple screen recorder in Python. To sum up the above program, we just create an infinite loop, and inside it, we keep capturing the screenshot and writing its data in the «recording.avi» file that makes a video. Also, to handle all the complex algorithms and conversion, we used Python pyautogui , opencv-python , and numpy libraries.

We hope you like the above article and if you have any queries related to the above program, please let us know by commenting down below.

People are also reading:

Источник

Оцените статью