Fast screenshots of (background) windows with, includes a find_window method
pip install handlescreenshots
Tested against Windows 10 / Python 3.11 / Anaconda
Win32WindowCapture Class
The Win32WindowCapture class provides functionality to capture screenshots of windows on the Windows operating system. It leverages the pywin32 module, along with NumPy for image processing.
Key Features:
Window Capture:
Captures screenshots of specified (background) windows.
Window Cropping:
Supports cropping of captured screenshots based on user-defined coordinates.
Frame Rate Display:
Optionally displays frames per second (FPS) during capture.
Color Space Conversion:
Supports conversion from BGR to RGB color space.
Error Handling:
Implements robust error handling to ensure stability during capture operations.
Class Methods:
__init__(self, hwnd: int, crop: Tuple[int, int, int, int] = (0, 0, 0, 0), show_fps: bool = False, brg_to_rgb: bool = False, ignore_exceptions: bool = True): Initializes a Win32WindowCapture instance with the specified window handle (hwnd). Additional parameters allow for customization of cropping, FPS display, color space conversion, and error handling.
get_window_position(self) -> Tuple[int, int, int, int]: Retrieves the position of the window and calculates its width and height.
get_screenshot(self, brg_to_rgb: Optional[bool] = None) -> Tuple[np.ndarray, Tuple[int, int, int, int], int, int, int, int]: Captures a screenshot of the window. Supports optional conversion from BGR to RGB color space.
__iter__(self) -> Iterable[Tuple[np.ndarray, Tuple[int, int, int, int], int, int, int, int, int, int]]: Implements an iterator that continuously captures screenshots of the window.
The find_window method provides a convenient way to locate windows based on various criteria defined in the searchdict parameter. This method offers several advantages:
Flexibility: By specifying different search parameters such as process ID, window title, window text, coordinates, class name, etc., users can locate windows in a variety of ways. This flexibility allows for precise window identification in diverse scenarios.
Customization: The searchdict parameter allows users to customize their search criteria according to their specific requirements. This customization empowers users to tailor window identification to the unique characteristics of their applications.
find_window
find_window(searchdict: Dict[str, Union[int, str, Tuple[int, int, int, int]]]) -> Dict[str, Union[int, str, Tuple[int, int, int, int]]]: Static method to locate windows based on specified search parameters. Returns information about the best-matching window.
all searchdict options:
searchdict = {
"pid": 1004,
"pid_re": "^1.*",
"title": "IME",
"title_re": "IM?E",
"windowtext": "Default IME",
"windowtext_re": r"Default\s*IME",
"hwnd": 197666,
"hwnd_re": r"\d+",
"length": 12,
"length_re": "[0-9]+",
"tid": 6636,
"tid_re": r"6\d+36",
"status": "invisible",
"status_re": "(?:in)?visible",
"coords_client": (0, 0, 0, 0),
"coords_client_re": r"\([\d,\s]+\)",
"dim_client": (0, 0),
"dim_client_re": "(1?0, 0)",
"coords_win": (0, 0, 0, 0),
"coords_win_re": r"\)$",
"dim_win": (0, 0),
"dim_win_re": "(1?0, 0)",
"class_name": "IME",
"class_name_re": "I?ME$",
"path": "C:\\Windows\\ImmersiveControlPanel\\SystemSettings.exe",
"path_re": "SystemSettings.exe",
}
How to use it
from handlescreenshots import Win32WindowCapture
import cv2
import time
searchdict = {"windowtext_re": r".*Bluestacks.*", "path_re": ".*HD-Player.*"}
bestwindow = Win32WindowCapture.find_window(searchdict)
print(bestwindow)
print(bestwindow["hwnd_of_best_window"])
try:
for pic in Win32WindowCapture(
bestwindow["hwnd_of_best_window"],
crop=(100, 20, 30, 40),
show_fps=False,
brg_to_rgb=False,
ignore_exceptions=True,
):
print(
"window_rect_not_cropped",
"offset_left",
"offset_top",
"w",
"h",
"end_x",
"end_y",
pic[1:],
end=" \r",
)
cv2.imshow(str(bestwindow["window_text_of_best_window"]), pic[0])
if cv2.waitKey(1) & 0xFF == ord("q"):
break
except KeyboardInterrupt:
try:
time.sleep(1)
except:
pass
cv2.destroyAllWindows()
Metadata
Release files for handlescreenshots 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| handlescreenshots-0.10.tar.gz | 24.9 kB | Without provenance | Details for handlescreenshots-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| handlescreenshots-0.10-py3-none-any.whl | Python 3 | none | any | Details for handlescreenshots-0.10-py3-none-any.whl |
Total release size: 50.1 kB
Release files / handlescreenshots-0.10.tar.gz
| Download URL | handlescreenshots-0.10.tar.gz |
|---|---|
| Size | 24.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
69aaeec05f9b6fba1395c415a38c72d90aebc5e188c22925040534981f165fbf
|
|
BLAKE2b-256 checksum How to use checksums |
7eb3bc46b6a1be7f31c1caa196d9e8f0fa54fdd1fce432d07fc54b0b281717a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / handlescreenshots-0.10-py3-none-any.whl
| Download URL | handlescreenshots-0.10-py3-none-any.whl |
|---|---|
| Size | 25.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70350e8f40af90eb991814f9a4c101243f46e14560b7c38832759428cb2ad9b9
|
|
BLAKE2b-256 checksum How to use checksums |
c38a99cd3be7eacfc74abf1bd473989ab12b35035af01a74ff08c888ceb4fed6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for handlescreenshots 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.