|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 |
| -# pylint: disable=missing-module-docstring |
4 |
| - |
5 |
| -from typing import List, NamedTuple |
6 |
| -import os |
7 | 3 | import logging
|
8 | 4 | import random
|
9 |
| -import re |
10 | 5 | import sys
|
11 | 6 | import time
|
| 7 | +from pathlib import Path |
12 | 8 |
|
13 |
| -from numpy import ndarray as NDArray |
14 | 9 | import click
|
15 |
| -import cv2 as cv # type: ignore |
16 |
| -import numpy as np |
17 |
| -import PIL # type: ignore |
18 |
| -import PIL.ImageOps # type: ignore |
19 |
| -import pyautogui # type: ignore |
| 10 | +import pyautogui |
| 11 | +from PIL import UnidentifiedImageError |
| 12 | +from PIL.Image import Image, open as open_image |
| 13 | +from pyautogui import ImageNotFoundException |
| 14 | +from pyscreeze import Box |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") |
20 | 17 |
|
21 | 18 |
|
22 | 19 | @click.command()
|
23 |
| -@click.option('--sleep_max', default=5.) |
24 |
| -@click.option('--sleep_min', default=0.) |
25 |
| -def run(sleep_max: float, sleep_min: float) -> None: # pylint: disable=missing-function-docstring |
26 |
| - logging.basicConfig( |
27 |
| - datefmt='%m/%d/%Y %I:%M:%S %p', |
28 |
| - format='%(asctime)s [%(levelname)s] %(message)s', |
29 |
| - level=logging.INFO, |
30 |
| - ) |
31 |
| - templates = _get_templates() |
32 |
| - while True: |
33 |
| - sleep_seconds = random.uniform(sleep_min, sleep_max) |
34 |
| - logging.info('Sleeping for %f seconds', sleep_seconds) |
35 |
| - time.sleep(sleep_seconds) |
| 20 | +@click.option("--confidence", default=0.7, show_default=True) |
| 21 | +@click.option("--grayscale/--color", default=True, show_default=True) |
| 22 | +@click.option("--min-sleep-interval", default=1, show_default=True) |
| 23 | +@click.option("--max-sleep-interval", default=5, show_default=True) |
| 24 | +@click.option("--templates-path", default=Path.cwd() / "templates", show_default=True) |
| 25 | +def main( |
| 26 | + confidence: float, |
| 27 | + grayscale: bool, |
| 28 | + min_sleep_interval: int, |
| 29 | + max_sleep_interval: int, |
| 30 | + templates_path: str, |
| 31 | +) -> None: |
| 32 | + templates_path_ = Path(templates_path) |
| 33 | + templates: dict[Path, Image] = {} |
| 34 | + for template_path in templates_path_.rglob("*"): |
36 | 35 | try:
|
37 |
| - _find_and_click(templates) |
38 |
| - except cv.error: # pylint: disable=no-member |
39 |
| - logging.info('Ignoring OpenCV error') |
40 |
| - |
| 36 | + templates[template_path] = open_image(template_path) |
| 37 | + except UnidentifiedImageError: |
| 38 | + logging.info(f"{template_path} is not a valid image; skipping") |
41 | 39 |
|
42 |
| -class _Template(NamedTuple): |
43 |
| - array: NDArray |
44 |
| - name: str |
45 |
| - threshold: int |
| 40 | + if len(templates) == 0: |
| 41 | + logging.error( |
| 42 | + f"No images found in {templates_path_.absolute()}. " |
| 43 | + f"If this is your first time running, take a screenshot and crop " |
| 44 | + f"(WIN+S on Windows) the item on the screen you want to click on, " |
| 45 | + f"placing the result in the {templates_path_.absolute()} directory." |
| 46 | + ) |
| 47 | + input("Press ENTER to exit.") |
| 48 | + sys.exit(1) |
46 | 49 |
|
| 50 | + while True: |
| 51 | + screenshot = pyautogui.screenshot() |
47 | 52 |
|
48 |
| -def _find_and_click(templates: List[_Template]) -> None: |
49 |
| - screenshot_image = pyautogui.screenshot() |
50 |
| - screenshot = _image_to_grayscale_array(screenshot_image) |
51 |
| - for template in templates: |
52 |
| - sift = cv.SIFT_create() # pylint: disable=no-member |
53 |
| - _, template_descriptors = sift.detectAndCompute(template.array, mask=None) |
54 |
| - screenshot_keypoints, screenshot_descriptors = sift.detectAndCompute(screenshot, mask=None) |
55 |
| - matcher = cv.BFMatcher() # pylint: disable=no-member |
56 |
| - matches = matcher.knnMatch(template_descriptors, screenshot_descriptors, k=2) |
57 |
| - points = np.array([screenshot_keypoints[m.trainIdx].pt for m, _ in matches if m.distance < template.threshold]) |
58 |
| - if points.shape[0] == 0: |
59 |
| - continue |
60 |
| - point = np.median(points, axis=0) |
61 |
| - current_mouse_pos = pyautogui.position() |
62 |
| - logging.info('Saving current mouse position at x=%f y=%f', *current_mouse_pos) |
63 |
| - pyautogui.click(*point) |
64 |
| - logging.info('Clicking on %s at coordinates x=%f y=%f', template.name, *point) |
65 |
| - pyautogui.moveTo(*current_mouse_pos) |
66 |
| - return |
67 |
| - logging.info('No matches found') |
68 |
| - |
69 |
| - |
70 |
| -def _get_templates() -> List[_Template]: # pylint: disable=too-many-locals |
71 |
| - templates = [] |
72 |
| - try: |
73 |
| - root_dir = sys._MEIPASS # type: ignore # pylint: disable=no-member,protected-access |
74 |
| - except AttributeError: |
75 |
| - root_dir = '.' |
76 |
| - templates_dir = os.path.join(root_dir, 'templates') |
77 |
| - pattern = re.compile(r'^([1-9][0-9]*)_([1-9][0-9]*)_(.+)\.png$') |
78 |
| - basenames = os.listdir(templates_dir) |
79 |
| - matches = (pattern.match(basename) for basename in basenames) |
80 |
| - filtered_matches = (match for match in matches if match is not None) |
81 |
| - groups = (match.groups() for match in filtered_matches) |
82 |
| - sorted_groups = sorted(groups, key=lambda t: int(t[0])) |
83 |
| - for index, threshold, name in sorted_groups: |
84 |
| - path = os.path.join(templates_dir, f'{index}_{threshold}_{name}.png') |
85 |
| - image = PIL.Image.open(path) # pylint: disable=no-member |
86 |
| - array = _image_to_grayscale_array(image) |
87 |
| - template = _Template(array=array, name=name, threshold=int(threshold)) |
88 |
| - templates.append(template) |
89 |
| - return templates |
90 |
| - |
| 53 | + for template_path, template_image in templates.items(): |
| 54 | + logging.info(f"Attempting to match {template_path}.") |
| 55 | + box: Box | None = None |
| 56 | + try: |
| 57 | + box = pyautogui.locate( |
| 58 | + template_image, |
| 59 | + screenshot, |
| 60 | + grayscale=grayscale, |
| 61 | + confidence=confidence, |
| 62 | + ) |
| 63 | + except ImageNotFoundException: |
| 64 | + pass |
| 65 | + if not isinstance(box, Box): |
| 66 | + continue |
| 67 | + match_x, match_y = pyautogui.center(box) |
| 68 | + pyautogui.click(match_x, match_y) |
| 69 | + logging.info(f"Matched at ({match_x}, {match_y}).") |
| 70 | + break |
91 | 71 |
|
92 |
| -def _image_to_grayscale_array(image: PIL.Image.Image) -> NDArray: |
93 |
| - image = PIL.ImageOps.grayscale(image) |
94 |
| - array = np.array(image) |
95 |
| - return array |
| 72 | + sleep_interval = random.uniform(min_sleep_interval, max_sleep_interval) |
| 73 | + logging.info(f"Waiting for {sleep_interval:.2f} seconds.") |
| 74 | + time.sleep(sleep_interval) |
96 | 75 |
|
97 | 76 |
|
98 |
| -if __name__ == '__main__': |
99 |
| - run() # pylint: disable=no-value-for-parameter |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
0 commit comments