bitmap — autopy module for working with bitmaps

This module defines the class Bitmap for accessing bitmaps and searching for bitmaps on-screen.

It also defines functions for taking screenshots of the screen.

Bitmap Object Methods

class autopy.bitmap.Bitmap
save(path: str, format: str=None)

Saves image to absolute path in the given format. The image type is determined from the filename if possible, unless format is given. If the file already exists, it will be overwritten. Currently only jpeg and png files are supported.

Exceptions:
  • IOError is thrown if the file could not be saved.

  • ValueError is thrown if image couldn’t be parsed.

copy_to_pasteboard()

Copies image to pasteboard. Currently only supported on macOS.

Exceptions:
  • IOError is thrown if the image could not be copied.

  • ValueError is thrown if the image was too large or small.

point_in_bounds(x: float, y: float) → bool

Returns True if the given point is contained in bmp.bounds.

rect_in_bounds(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → bool

Returns True if the given rect of the form ((x, y), (width, height)) is contained in bmp.bounds.

open(path: str) → Bitmap

Open the image located at the path specified. The image’s format is determined from the path’s file extension.

get_color(x: float, y: float) → Tuple[int, int, int]

Returns hexadecimal value describing the color at a given point.

Exceptions:
  • ValueError is thrown if the point out of bounds.

find_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → Tuple[float, float]

Attempts to find color inside rect of the form ((x, y), (width, height)) in bmp from the given start_point. Returns coordinates if found, or None if not. If rect is None, bmp.bounds is used instead. If start_point is None, the origin of rect is used.

Tolerance is defined as a float in the range from 0 to 1, where 0 is an exact match and 1 matches anything.

find_every_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → List[Tuple[float, float]]

Returns list of all (x, y) coordinates inside rect in bmp matching color from the given start_point. If rect is None, bmp.bounds is used instead. If start_point is None, the origin of rect is used.

count_of_color(color: Tuple[int, int, int], tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → int

Returns count of color in bitmap. Functionally equivalent to:

len(find_every_color(color, tolerance, rect, start_point))

find_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → Tuple[float, float]

Attempts to find needle inside rect in bmp from the given start_point. Returns coordinates if found, or None if not. If rect is None, bmp.bounds is used instead. If start_point is None, the origin of rect is used.

Tolerance is defined as a float in the range from 0 to 1, where 0 is an exact match and 1 matches anything.

find_every_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → [Tuple[float, float]]

Returns list of all (x, y) coordinates inside rect in bmp matching needle from the given start_point. If rect is None, bmp.bounds is used instead. If start_point is None, the origin of rect is used.

count_of_bitmap(needle: Bitmap, tolerance: float=None, rect: Tuple[Tuple[float, float], Tuple[float, float]]=None, start_point: Tuple[float, float]=None) → int

Returns count of occurrences of needle in bmp. Functionally equivalent to:

len(find_every_bitmap(color, tolerance, rect, start_point))

cropped(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → Bitmap

Returns new bitmap object created from a portion of another.

Exceptions:
  • ValueError is thrown if the portion was out of bounds.

is_bitmap_equal(bitmap: Bitmap, tolerance: float=None) → bool

Returns true if bitmap is equal to receiver with the given tolerance.

Functions

autopy.bitmap.capture_screen(rect: Tuple[Tuple[float, float], Tuple[float, float]]) → autopy.bitmap.Bitmap

Returns a screengrab of the given portion of the main display, or the entire display if rect is None. The rect parameter is in the form of ((x, y), (width, height)).

Exceptions:
  • ValueError is thrown if the rect is out of bounds.

  • IOError is thrown if the image failed to parse.