Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Examples

Watchdog0x edited this page Aug 16, 2023 · 4 revisions

Example 1: Capture Screen Data

This example demonstrates how to capture screen data from a specific screen using the ScreenCapture class.

from screen_capture import GetScreen, ScreenCapture  # Assuming you've saved the code in a file named "screen_capture.py"

def main():
    screen_number = 0  # Change this to the desired screen number
    screen = GetScreen(screen_number)
    
    capture = ScreenCapture(screen)
    
    print("Capturing screen data...")
    captured_data = capture.get_captured_data()
    
    with open("captured_screen.bmp", "wb") as bmp_file:
        bmp_file.write(captured_data)
    
    print("Screen data captured and saved as captured_screen.bmp")

if __name__ == "__main__":
    main()

Example 2: Get Display Monitor Information

This example demonstrates how to retrieve and print information about available display monitors using the GetScreen class.

from screen_capture import GetScreen  # Assuming you've saved the code in a file named "screen_capture.py"

def main():
    screen_number = 0  # Change this to the desired screen number
    screen = GetScreen(screen_number)
    
    print("Display Name:", screen.display_name)
    print("Primary Screen:", screen.is_primary_screen)
    print("Screen Coordinates (left, top, right, bottom):", screen.left_top_right_bottom)
    print("Screen Size (x, y, width, height):", screen.x_y_width_height)

if __name__ == "__main__":
    main()

Example 3: Capture Screenshots from Multiple Monitors

This example demonstrates how to capture screenshots from multiple monitors and save them as separate image files.

from screen_capture import GetScreen, ScreenCapture  # Assuming you've saved the code in a file named "screen_capture.py"

def capture_and_save_screen(screen_number):
    screen = GetScreen(screen_number)
    capture = ScreenCapture(screen)
    
    print(f"Capturing screen {screen_number}...")
    captured_data = capture.get_captured_data()
    
    file_name = f"captured_screen_{screen_number}.bmp"
    with open(file_name, "wb") as bmp_file:
        bmp_file.write(captured_data)
    
    print(f"Screen {screen_number} captured and saved as {file_name}")

def main():
    num_screens = 2  # Change this to the number of screens you want to capture
    for screen_number in range(num_screens):
        capture_and_save_screen(screen_number)

if __name__ == "__main__":
    main()

Example 4: Convert the bitmap_data into a NumPy array

from screen_manager import GetScreen
import numpy as np
import cv2


screen = GetScreen(1)
_, _, width, height = screen.x_y_width_height
start = ScreenCapture(screen.get_monitor)

while True:
    data = start.get_captured_data()
    image = np.frombuffer(data, dtype=np.uint8).reshape((height, width, 3))
    cv2.imshow("OpenCV/Numpy normal", image)
    if cv2.waitKey(25) & 0xFF == ord("q"):
        cv2.destroyAllWindows()
        break