PyScreeze Documentation

repository·master·Indexed 42 days ago

https://github.com/asweigart/pyscreeze

A cross-platform Python 3 module for taking screenshots, saving them to files, and locating specific images on the screen. It provides functions like screenshot(), pixel(), and pixelMatchesColor(), as well as image location tools such as locateOnScreen(), locateCenterOnScreen(), and locateAllOnScreen(). The module depends on Pillow and requires specific system dependencies for Linux (scrot) and Ubuntu (python3-xlib).

Tokens
1.8K
Snippets
8
Records
9
Agent score
3%

What's inside PyScreeze

  1. Use grayscale matching for faster location

    master

    You can pass grayscale=True to any locate function. This desaturates the images and screenshots, which can provide a speedup of approximately 30%. However, this may increase the risk of false-positive matches.

    import pyautogui
    
    # Faster matching using grayscale
    location = pyautogui.locateOnScreen('calc7key.png', grayscale=True)
  2. Install dependencies for PyScreeze

    master

    PyScreeze requires the Pillow module for screenshot functionality.

    OS Specific Requirements:

    • macOS: Uses the built-in screencapture command.
    • Linux: Requires scrot. Install it via:
      sudo apt-get install scrot
    - **Ubuntu:** Note that Ubuntu may have issues with Pillow installations where PNG and JPEG support are not included by default.
    
    sudo apt-get install scrot
  3. Security note regarding Pillow dependency

    master

    PyScreeze depends on the Pillow library for screenshot capabilities.

    Warning: Older versions of Pillow have known security issues. It is highly recommended to use Pillow version 8.3.2 or later. Note that Pillow 8.3.2 requires at least Python 3.6. If you are using Python 3.5 or earlier, you may be forced to use an insecure version of Pillow.

  4. Get pixel color and verify pixel color

    master

    You can retrieve the RGB color of a specific pixel or verify if a pixel matches a specific color.

    • pixel(x, y): Returns the RGB tuple for the pixel at the given coordinates.
    • pixelMatchesColor(x, y, (R, G, B), tolerance=0): Returns True if the pixel at (x, y) matches the provided RGB tuple within the specified tolerance. The tolerance defines how much each R, G, and B value can vary.
    import pyautogui
    
    # Get RGB color of a pixel
    color = pyautogui.pixel(100, 200)
    
    # Verify if a pixel matches a color with tolerance
    # tolerance=10 allows R, G, and B to vary by up to 10
    matches = pyautogui.pixelMatchesColor(100, 200, (130, 135, 144), tolerance=10)
  5. Locate images on screen

    master

    PyScreeze provides several functions to find the coordinates of an image (the 'needle') within the screen or another image (the 'haystack').

    Core Locate Functions:

    • locateOnScreen(image, grayscale=False): Returns the first instance found as a (left, top, width, height) tuple. Returns None if not found.
    • locateCenterOnScreen(image, grayscale=False): Returns the (x, y) coordinates of the center of the first instance found. Returns None if not found.
    • locateAllOnScreen(image, grayscale=False): Returns a generator yielding (left, top, width, height) tuples for every instance found.
    • locate(needleImage, haystackImage, grayscale=False): Returns the (left, top, width, height) of the first instance of needleImage within haystackImage.
    • locateAll(needleImage, haystackImage, grayscale=False): Returns a generator yielding (left, top, width, height) tuples for all instances of needleImage in haystackImage.
    • locateOnScreen(image, title): Returns the coordinates of the image within a specific window identified by title. Requires PyGetWindow to be installed.

    Performance Note: Locating images typically takes 1-2 seconds on a 1920x1080 screen.

    import pyautogui
    
    # Get bounding box
    location = pyautogui.locateOnScreen('button.png')
    
    # Get center coordinates for clicking
    x, y = pyautogui.locateCenterOnScreen('button.png')
    pyautogui.click(x, y)
    
    # Iterate through all matches
    for pos in pyautogui.locateAllOnScreen('button.png'):
        print(pos)
  6. Get pixel color and verify pixel matches

    master

    You can retrieve the RGB color of a specific pixel or verify if a pixel matches a target color.

    • pixel(x, y): Returns the RGB tuple for the pixel at coordinates (x, y).
    • pixelMatchesColor(x, y, (R, G, B), tolerance=0): Returns True if the pixel at (x, y) matches the provided RGB tuple. The tolerance argument allows for a specified variance in each R, G, and B channel.
    import pyscreeze
    
    # Get RGB color of a pixel
    color = pyscreeze.pixel(100, 200)
    
    # Check if a pixel matches a specific color
    matches = pyscreeze.pixelMatchesColor(100, 200, (130, 135, 144))
    
    # Check if a pixel matches with a tolerance
    matches_with_tolerance = pyscreeze.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
  7. Take screenshots with screenshot()

    master

    Use pyscreeze.screenshot() to capture the entire screen. It returns a Pillow Image object. To save the screenshot directly to a file, pass a filename string as the first argument. You can also capture a specific portion of the screen using the region keyword argument, which accepts a four-integer tuple: (left, top, width, height).

    import pyscreeze
    
    # Capture entire screen as an Image object
    im1 = pyscreeze.screenshot()
    
    # Capture entire screen and save to a file
    im2 = pyscreeze.screenshot('my_screenshot.png')
    
    # Capture a specific region (left, top, width, height)
    im = pyscreeze.screenshot(region=(0, 0, 300, 400))
  8. Capture screenshots with screenshot()

    master

    Use pyautogui.screenshot() to capture the screen.

    • Calling screenshot() returns a Pillow Image object.
    • Passing a filename string (e.g., 'my_screenshot.png') saves the file to disk and returns the Image object.
    • Use the region keyword argument to capture a specific part of the screen. The region must be a four-integer tuple: (left, top, width, height).
    import pyautogui
    
    # Capture entire screen as Image object
    im1 = pyautogui.screenshot()
    
    # Capture and save to file
    im2 = pyautogui.screenshot('my_screenshot.png')
    
    # Capture a specific region (left, top, width, height)
    im = pyautogui.screenshot(region=(0, 0, 300, 400))