Beispiel #1
0
    def target_offset(self, dx: int, dy: int):
        """Add offset to Pattern from top left.

        :param int dx: x offset from center.
        :param int dy: y offset from center.
        :return: A new pattern object.
        """
        new_pattern = Pattern(self.image_name, from_path=self.image_path)
        new_pattern._target_offset = Location(dx, dy)
        return new_pattern
Beispiel #2
0
def match_template(pattern: Pattern, region=None) -> Location:
    """Find a pattern in a Region or full screen

    :param Pattern pattern: Image details
    :param Region region: Region object.
    :return: Location.
    """
    logger.debug('Searching for pattern: %s' % pattern.get_filename())
    try:
        stack_image = ScreenshotImage(region=region)
        precision = pattern.similarity

        needle = pattern.get_color_image(
        ) if precision == 0.99 else pattern.get_gray_image()
        haystack = stack_image.get_color_image(
        ) if precision == 0.99 else stack_image.get_gray_image()

        res = cv2.matchTemplate(np.array(haystack), np.array(needle),
                                FIND_METHOD)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

        if max_val < precision:
            position = Location(-1, -1)
        else:
            position = Location(max_loc[0], max_loc[1])

        # if position.x == -1:
        #     save_debug_image(needle, np.array(haystack), None, True)
        # else:
        #     save_debug_image(needle, haystack, position)
    except ScreenshotError:
        logger.warning('Screenshot failed.')
        position = Location(-1, -1)

    if position.x == -1 or position.y == -1:
        return position
    elif region is not None:
        return Location(position.x + region.x, position.y + region.y)
    else:
        return position
Beispiel #3
0
def match_template_multiple(pattern: Pattern,
                            region=None,
                            threshold: int = 0.99) -> List[Location]:
    logger.debug('Searching for pattern: %s' % pattern.get_filename())
    try:
        stack_image = ScreenshotImage(region=region)
        precision = pattern.similarity

        needle = pattern.get_rgb_array(
        ) if precision == 0.99 else pattern.get_gray_array()
        haystack = stack_image.get_rgb_array(
        ) if precision == 0.99 else stack_image.get_gray_array()

        res = cv2.matchTemplate(haystack, needle, FIND_METHOD)

        final_matches = []
        while True:
            min_val, max_val, min_loc, best_match = cv2.minMaxLoc(res)
            if FIND_METHOD in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
                top_left = min_loc
            else:
                top_left = best_match

            if precision > max_val > threshold:
                sx, sy = top_left
                for x in range(sx - pattern.get_size().width / 2,
                               sx + pattern.get_size().width / 2):
                    for y in range(sy - pattern.get_size().height / 2,
                                   sy + pattern.get_size().height / 2):
                        try:
                            res[y][x] = np.float32(-10000)
                        except IndexError:
                            pass
                new_match_point = Location(top_left[0], top_left[1])
                final_matches.append(new_match_point)
            else:
                break
        return final_matches
    except ScreenshotError:
        logger.warning('Screenshot failed.')
        return []
Beispiel #4
0
 def get_bottom_right(self) -> Location:
     """Returns a Location object for the bottom right of te screen."""
     return Location(self.x + self.width, self.y + self.height)
Beispiel #5
0
 def get_bottom_left(self) -> Location:
     """Returns a Location object for the bottom left of te screen."""
     return Location(self.x, self.y + self.height)
Beispiel #6
0
 def get_top_right(self) -> Location:
     """Returns a Location object for the top right of te screen."""
     return Location(self.x + self.width, self.y)
Beispiel #7
0
 def get_top_left(self) -> Location:
     """Returns a Location object for the top left of te screen."""
     return Location(self.x, self.y)
Beispiel #8
0
 def get_center(self) -> Location:
     """Returns a Location object for the center of te screen."""
     return Location(int((self.x + self.width) / 2),
                     int((self.y + self.height) / 2))