BetterCam: The Fastest Python Screenshot Library for Windows
In the world of Python libraries, BetterCam stands out as the fastest publicly available screenshot library for Windows. This powerful library, built on the foundation of DXCam, provides blazing-fast screen capturing capabilities using the Desktop Duplication API. It surpasses other Python solutions like python-mss and D3DShot and is a perfect fit for deep learning pipelines in first-person shooter (FPS) games.
Introduction
BetterCam boasts a range of impressive features that make it the top choice for developers working with high-speed screen capturing:
- Insanely fast screen capturing at over 240Hz
- Capture from Direct3D exclusive full-screen apps without interruption, even during alt+tab
- Auto-adjust to scaled / stretched resolutions
- Precise FPS targeting for video output
- Smooth integration with popular Python libraries like NumPy, OpenCV, and PyTorch
The BetterCam community also thrives on community contributions, making it a collaborative and dynamic project.
Installation
Installing BetterCam is a breeze. Simply use pip to install it from the Python Package Index (PyPI):
pip install bettercam
Please note that you will also need to install OpenCV if it’s not already available by running pip install opencv-python
.
Usage
To get started with BetterCam, you need to create a BetterCam
instance for each monitor you want to capture:
python
import bettercam
camera = bettercam.create() # Primary monitor's BetterCam instance
Screenshot
To take a quick screenshot, call the .grab
method:
python
frame = camera.grab()
The frame
variable will contain a numpy.ndarray
in the (Height, Width, 3[RGB])
format by default. You can display the screenshot using a library like PIL:
python
from PIL import Image
Image.fromarray(frame).show()
If you want to capture a specific region, you can provide the region
parameter with the bounding box coordinates:
python
left, top = (1920 - 640) // 2, (1080 - 640) // 2
right, bottom = left + 640, top + 640
region = (left, top, right, bottom)
frame = camera.grab(region=region) # A 640x640x3 numpy ndarray snapshot
Screen Capture
To start and stop screen capture, use the .start
and .stop
methods:
“`python
camera.start(region=(left, top, right, bottom)) # Capture a region (optional)
camera.is_capturing # True
… Your Code
camera.stop()
camera.is_capturing # False
“`
Retrieving Captured Data
While capturing, you can grab the latest frame using the .get_latest_frame
method:
python
camera.start()
for i in range(1000):
image = camera.get_latest_frame() # Waits for a new frame
camera.stop()
Advanced Usage & Notes
For advanced scenarios, BetterCam provides additional features and notes to enhance your experience.
Multiple Monitors / GPUs
With BetterCam, you can work with multiple monitors or GPUs by creating separate instances:
python
cam1, cam2, cam3 = [bettercam.create(device_idx=d, output_idx=o) for d, o in [(0, 0), (0, 1), (1, 1)]]
img1, img2, img3 = [cam.grab() for cam in (cam1, cam2, cam3)]
You can also use the bettercam.device_info()
and bettercam.output_info()
methods to list available devices and outputs.
Output Format
When creating a BetterCam instance, you can specify the color mode with the output_color
parameter:
python
bettercam.create(output_idx=0, output_color="BGRA")
Supported color modes include “RGB”, “RGBA”, “BGR”, “BGRA”, and “GRAY” for grayscale. Only numpy.ndarray
shapes are supported: (Height, Width, Channels)
.
Video Buffer
BetterCam uses a fixed-size ring buffer to store frames. You can customize its maximum length using the max_buffer_len
parameter during creation:
python
camera = bettercam.create(max_buffer_len=512)
Target FPS
For precise FPS targeting, BetterCam utilizes the high-resolution CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
. You can specify the target FPS during the .start
method:
python
camera.start(target_fps=120) # Ideally, not beyond 240Hz.
Video Mode
To record constant framerate videos, set video_mode=True
during the .start
method:
“`python
Example: Record a 5-second, 120Hz video
camera.start(target_fps=target_fps, video_mode=True)
… Video writing code goes here
“`
Resource Management
To stop captures and free resources, call the .release
method. Manual deletion is also possible:
python
del camera
Benchmarks
BetterCam’s performance shines through in various benchmarks. Below are some benchmark results comparing BetterCam with other popular solutions:
Max FPS Achievement:
| | BetterCam Nvidia GPU | BetterCam | DXCam | python-mss | D3DShot |
|———–|————————|————|——-|————|———|
| Avg FPS | 111.667 | 123.667 | 39 | 34.667 | N/A |
| Std Dev | 0.889 | 1.778 | 1.333 | 2.222 | N/A |
FPS Targeting:
| Target/Result | BetterCam Nvidia GPU | BetterCam | DXCam | python-mss | D3DShot |
|—————|————————|————|——-|————|———|
| 120fps | 111.667, 0.889 | 88.333, 2.444 | 36.667, 0.889 | N/A | N/A |
| 60fps | 60, 0 | 60, 0 | 35, 5.3 | N/A | N/A |
Referenced Work
BetterCam builds upon the work of other projects that have contributed to its development and inspiration. These include:
- DXCam: Our origin story.
- D3DShot: Provided foundational ctypes.
- OBS Studio: A treasure trove of knowledge.
In conclusion, BetterCam is an impressive Python screenshot library for Windows that offers unmatched speed and efficiency. With its unique features and integration capabilities, it outshines other solutions in the market. Whether you are a developer working on FPS games, deep learning projects, or any application that requires high-speed screen capturing, BetterCam is the perfect choice.
Give BetterCam a try today and experience the world’s fastest Python screenshot library for Windows!
References:
Leave a Reply