[파이썬] pygame 스크린샷 및 비디오 캡쳐

Introduction

pygame is a popular library for developing games and multimedia applications in Python. It provides various features and functions to create interactive experiences. One of the essential aspects of game development is being able to capture screenshots or record videos of your game to share with others or for documentation purposes. In this blog post, we will explore how to take screenshots and capture videos using pygame.

Taking Screenshots

To take a screenshot of your pygame window, you can use the pygame.image.save() function. This function saves the current state of your game window as an image file. Here is an example code snippet demonstrating how to take a screenshot:

import pygame

pygame.init()

# Create a pygame window
window_width, window_height = 800, 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Screenshot Example")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Draw objects and update the game window here

    # Capture a screenshot
    pygame.image.save(window, "screenshot.png")

pygame.quit()

In the above code, we create a pygame window and continuously update it until the user closes the window. Inside the main loop, we draw objects and perform other game logic. To capture a screenshot, we call the pygame.image.save() function and pass the pygame window surface along with the desired file name as arguments. This will save the current state of the game window as a PNG image file.

Capturing Videos

To capture a video of your pygame application, you will need to install the pyautogui library, which provides cross-platform, screen-capturing functions. You can install it via pip with the following command:

pip install pyautogui

Here is an example code snippet demonstrating how to capture a video of your pygame application:

import pygame
import pyautogui
import cv2

pygame.init()

# Create a pygame window
window_width, window_height = 800, 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Video Capture Example")

# Set up video recording
fourcc = cv2.VideoWriter_fourcc(*"XVID")
fps = 30.0
video_filename = "capture.avi"
video_out = cv2.VideoWriter(video_filename, fourcc, fps, (window_width, window_height))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # Draw objects and update the game window here

    # Capture the current frame and write it to the video file
    screenshot = pygame.surfarray.array3d(window)
    screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR)
    video_out.write(screenshot)

pygame.quit()
video_out.release()

In this code, we import the necessary libraries cv2 (OpenCV for video writing) and pyautogui. We set up the video recording parameters such as the video filename, codec, FPS, and window size. Inside the main loop, we draw objects, update the game window, and then capture the current frame by converting the pygame surface to an OpenCV-compatible format. Finally, we write the captured frame to the video file using the VideoWriter.write() function.

Conclusion

Capturing screenshots and videos in pygame allows you to showcase your game or capture moments during development. The pygame.image.save() function enables you to easily save screenshots, while the pyautogui library combined with OpenCV provides a reliable way to capture videos. Experiment with these techniques and enhance the presentation and documentation of your pygame applications.