[파이썬] opencv-python 스크린 캡처 및 스크린 레코딩

In this tutorial, we will explore how to capture screenshots and record the screen using OpenCV-Python. OpenCV is a popular computer vision library that provides various functions for image and video processing.

Installing OpenCV-Python

To get started, we need to install the OpenCV-Python library. You can install it using pip by running the following command:

pip install opencv-python

Capturing a screenshot

Capturing a screenshot with OpenCV-Python is quite straightforward. We can use the cv2.imread() function to read the screen image and display it using the cv2.imshow() function.

Here’s an example code that captures a screenshot and displays it:

import cv2
import pyautogui

# Get the screen dimensions
screen_info = pyautogui.screen_info()
width, height = screen_info['width'], screen_info['height']

# Capture the screenshot
screenshot = pyautogui.screenshot()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)

# Display the screenshot
cv2.imshow("Screenshot", screenshot)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above code, we first import the necessary libraries. We use the pyautogui library to get the screen dimensions and capture the screenshot. Then, we convert the screenshot image to the BGR format (required by OpenCV) using the cv2.cvtColor() function. Finally, we display the screenshot using cv2.imshow().

Recording the screen

Recording the screen using OpenCV-Python follows a similar approach to capturing the screenshot. Instead of capturing a single screenshot, we will continuously capture frames and write them to a video file.

Here’s an example code that records the screen and saves it as a video file:

import cv2
import pyautogui

# Get the screen dimensions
screen_info = pyautogui.screen_info()
width, height = screen_info['width'], screen_info['height']

# Define the output video file
output_file = "screen_record.mp4"

# Define the video codec
fourcc = cv2.VideoWriter_fourcc(*"mp4v")

# Initialize the video writer
video_writer = cv2.VideoWriter(output_file, fourcc, 20.0, (width, height))

# Record the screen
while True:
    # Capture the frame
    frame = pyautogui.screenshot()
    frame = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
    
    # Write the frame to the video
    video_writer.write(frame)
    
    # Break the loop when 'q' is pressed
    if cv2.waitKey(1) == ord('q'):
        break

# Release the video writer
video_writer.release()
cv2.destroyAllWindows()

In the above code, we first import the necessary libraries and define the output video file name. We use the cv2.VideoWriter() function to initialize the video writer, specifying the output file, codec, frame rate, and dimensions. Inside the loop, we continuously capture frames using pyautogui.screenshot(), convert the frames to BGR format, and write them to the video file using video_writer.write(). The loop breaks when the ‘q’ key is pressed. Finally, we release the video writer and close any open windows.

Conclusion

In this tutorial, we learned how to capture screenshots and record the screen using OpenCV-Python. We explored the functions provided by OpenCV and demonstrated how to use them to capture, display, and save screen images and videos. With this knowledge, you can now integrate screen capturing and recording capabilities into your Python projects.