PyScreeze
PyScreeze is a simple, cross-platform screenshot module for Python 3. It provides functionality to take screenshots, save them to files, and locate images within the screen. This is particularly useful for GUI automation tasks where visual elements need to be identified. The library is currently at version 1.0.1 and typically has an infrequent release cadence, with the last PyPI release in August 2024.
Warnings
- breaking The default behavior of `locate` functions changed in version 1.0.0. They now raise `pyscreeze.ImageNotFoundException` when an image is not found, instead of returning `None`.
- gotcha Pillow version compatibility is crucial. Older versions of Pillow have known security vulnerabilities. For Python 3.6+, Pillow >= 8.3.2 is recommended. For Python 3.7+ and Wayland support on Linux, Pillow >= 9.2.0 is needed.
- gotcha On Linux, PyScreeze often relies on external command-line tools for screenshots (e.g., `scrot` or `gnome-screenshot`) and may require `python3-xlib` for X11 environments. These must be installed separately via the system's package manager.
- gotcha When used with PyAutoGUI, a common issue is 'PyAutoGUI was unable to import PyScreeze' errors. This often indicates problems with PyScreeze or Pillow installations, or Python environment conflicts.
- breaking The `pixelMatchesColor()` function in version 1.0.0 changed its argument signature. It no longer accepts a `(x, y)` tuple as the first argument; `x` and `y` must now be passed as separate arguments.
- gotcha The `region` argument for `screenshot()` must be a tuple of exactly four integers (left, top, width, height). Non-integer values or incorrect lengths will raise an error.
Install
-
pip install pyscreeze
Imports
- pyscreeze
import pyscreeze
Quickstart
import pyscreeze
# Take a screenshot of the entire screen
im1 = pyscreeze.screenshot()
# Save a screenshot to a file
im2 = pyscreeze.screenshot('my_screenshot.png')
# Take a screenshot of a specific region (left, top, width, height)
# Ensure 'image_path.png' exists for locateOnScreen to work
try:
region_screenshot = pyscreeze.screenshot(region=(0, 0, 300, 400))
print(f"Screenshot of region saved as temporary file. Image object: {region_screenshot}")
# Locate an image on the screen
# Replace 'path/to/your/button.png' with an actual image file on your screen
# Ensure an image 'button.png' is in the current directory or provide full path
button_location = pyscreeze.locateOnScreen('button.png', confidence=0.8)
if button_location:
print(f"Button found at: {button_location}")
center_coords = pyscreeze.center(button_location)
print(f"Center of button: {center_coords}")
else:
print("Button not found on screen.")
except pyscreeze.ImageNotFoundException:
print("Image not found on screen (expected for 'button.png' if not present).")
except FileNotFoundError:
print("Ensure 'button.png' exists in the current directory or provide a full path.")
except Exception as e:
print(f"An error occurred: {e}")