def find(image_name, region=None): """Look for a single match of a Pattern or image. :param image_name: String or Pattern. :param region: Region object in order to minimize the area. :return: Location object. """ if isinstance(image_name, Pattern): image_found = image_search(image_name, region) if (image_found.x != -1) & (image_found.y != -1): if parse_args().highlight: highlight(region=region, pattern=image_name, location=image_found) return image_found else: raise FindError('Unable to find image %s' % image_name.get_filename()) elif isinstance(image_name, str): a_match = text_search_by(image_name, True, region) if a_match is not None: return Location(a_match['x'] + a_match['width'] / 2, a_match['y'] + a_match['height'] / 2) else: raise FindError('Unable to find text %s' % image_name) else: raise ValueError(INVALID_GENERIC_INPUT)
def find(image_name, precision=None, in_region=None): """Look for a single match of a Pattern or image :param image_name: String or Pattern :param precision: Matching similarity :param in_region: Region object in order to minimize the area :return: Location """ if isinstance(image_name, str) and is_ocr_text(image_name): a_match = text_search_by(image_name, True, in_region) if a_match is not None: return Location(a_match['x'] + a_match['width'] / 2, a_match['y'] + a_match['height'] / 2) else: raise FindError('Unable to find text %s' % image_name) elif isinstance(image_name, str) or isinstance(image_name, Pattern): if precision is None: precision = Settings.MinSimilarity try: pattern = Pattern(image_name) except Exception: pattern = image_name image_found = image_search(pattern, precision, in_region) if (image_found.x != -1) & (image_found.y != -1): return image_found else: raise FindError('Unable to find image %s' % image_name) else: raise ValueError(INVALID_GENERIC_INPUT)
def hover(where=None, duration=0, in_region=None): """Hover over a Location, Pattern or image :param where: Location, Pattern or image name for hover target :param duration: speed of hovering from current location to target :param in_region: Region object in order to minimize the area :return: None """ if isinstance(where, str) and is_ocr_text(where): a_match = text_search_by(where, True, in_region) if a_match is not None: pyautogui.moveTo(a_match['x'] + a_match['width'] / 2, a_match['y'] + a_match['height'] / 2) else: raise FindError('Unable to find text %s' % where) elif isinstance(where, Location): pyautogui.moveTo(where.x, where.y, duration) elif isinstance(where, str) or isinstance(where, Pattern): try: pattern = Pattern(where) except Exception: pattern = where pos = image_search(pattern, region=in_region) if pos.x != -1: needle_width, needle_height = get_image_size(pattern.getFilename()) if isinstance(where, Pattern): possible_offset = where.getTargetOffset() if possible_offset is not None: move_to = Location(pos.getX() + possible_offset.getX(), pos.getY() + possible_offset.getY()) pyautogui.moveTo(move_to.getX(), move_to.y) else: move_to = Location(pos.x, pos.y) pyautogui.moveTo(move_to.getX() + needle_width / 2, move_to.getY() + needle_height / 2) else: pyautogui.moveTo(pos.x + needle_width / 2, pos.y + needle_height / 2) else: raise FindError('Unable to find image %s' % pattern.getFilename()) else: raise ValueError(INVALID_GENERIC_INPUT)
def hover(where=None, duration=0, in_region=None): """Hover over a Location, Pattern or image. :param where: Location, Pattern or image name for hover target. :param duration: Speed of hovering from current location to target. :param in_region: Region object in order to minimize the area. :return: None. """ if isinstance(where, Pattern): pos = image_search(where, region=in_region) if pos.x != -1: needle_width, needle_height = where.get_size() if isinstance(where, Pattern): possible_offset = where.get_target_offset() if possible_offset is not None: move_to = Location(pos.x + possible_offset.x, pos.y + possible_offset.y) pyautogui.moveTo(move_to.x, move_to.y) else: move_to = Location(pos.x, pos.y) pyautogui.moveTo(move_to.x + needle_width / 2, move_to.y + needle_height / 2) else: pyautogui.moveTo(pos.x + needle_width / 2, pos.y + needle_height / 2) else: raise FindError('Unable to find image %s' % where.get_filename()) elif isinstance(where, str): ocr_search = OCRSearch() a_match = ocr_search.text_search_by(where, True, in_region) if a_match is not None: pyautogui.moveTo(a_match['x'] + a_match['width'] / 2, a_match['y'] + a_match['height'] / 2) else: raise FindError('Unable to find text %s' % where) elif isinstance(where, Location): pyautogui.moveTo(where.x, where.y, duration) else: raise ValueError(INVALID_GENERIC_INPUT)
def _to_location(ps=None, in_region=None, align='top_left'): """Transform pattern or string to location :param ps: Pattern or string input :param in_region: Region object in order to minimize the area :param align: Alignment could be top_left, center :return: Location object """ # TODO: Add multiple alignments if needed # TODO fix this (isinstance str or Pattern) if isinstance(ps, Location): return ps elif isinstance(Pattern(ps), Pattern): location = image_search(Pattern(ps), Settings.MinSimilarity, in_region) if align == 'center': width, height = get_image_size(Pattern(ps)) return Location(location.getX() + width / 2, location.getY() + height / 2) else: return location