commit e949d13f0b5cc70f01c6e760f278aa71872a6320 Author: shikong <919411476@qq.com> Date: Thu Sep 14 02:40:30 2023 +0800 简单试水 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..afa4cd3 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Zeppelin 忽略的文件 +/ZeppelinRemoteNotebooks/ diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d0b5817 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3e0c1c5 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/py-autogui.iml b/.idea/py-autogui.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/py-autogui.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/assets/images/test.png b/assets/images/test.png new file mode 100644 index 0000000..bc58e95 Binary files /dev/null and b/assets/images/test.png differ diff --git a/assets/images/win_win10.png b/assets/images/win_win10.png new file mode 100644 index 0000000..5c97cf8 Binary files /dev/null and b/assets/images/win_win10.png differ diff --git a/func/__init__.py b/func/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/func/location.py b/func/location.py new file mode 100644 index 0000000..3a2b537 --- /dev/null +++ b/func/location.py @@ -0,0 +1,12 @@ +import time + +import pyautogui + + +def print_location(): + while True: + x, y = pyautogui.position() + position_str = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) + print(position_str, end='') + time.sleep(0.1) + print('\b' * len(position_str), end='', flush=True) diff --git a/main.py b/main.py new file mode 100644 index 0000000..0548765 --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +from PIL import Image +from mss import * +from pyautogui import * + +import utils.cv2 + +FAILSAFE = False + +screen_size = size() +screen_x, screen_y = screen_size.width, screen_size.height +if __name__ == '__main__': + if not os.path.exists("snap"): + os.mkdir("snap") + + with mss() as m: + monitors = m.monitors[:1] + for (i, monitor) in enumerate(monitors): + print(monitor) + img = m.grab(monitor) + pim = Image.new("RGB", img.size) + pim.frombytes(img.rgb) + # loc = utils.cv2.match(pim, "assets/images/win_win10.png") + # if loc is not None: + # x, y = loc + # moveTo(x, y) + loc = utils.cv2.match_resize(pim, "assets/images/win_win10.png", 0.8) + if loc is not None: + x, y = loc + moveTo(x, y) + print(screen_x, screen_y) + + + diff --git a/snap/grab0.jpg b/snap/grab0.jpg new file mode 100644 index 0000000..0ff4a2e Binary files /dev/null and b/snap/grab0.jpg differ diff --git a/snap/grab1.jpg b/snap/grab1.jpg new file mode 100644 index 0000000..3a27af3 Binary files /dev/null and b/snap/grab1.jpg differ diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/cv2.py b/utils/cv2.py new file mode 100644 index 0000000..d8a03f8 --- /dev/null +++ b/utils/cv2.py @@ -0,0 +1,76 @@ +import cv2 +import imutils +import numpy as np +from PIL.Image import Image + +import utils.matplotlib + + +def match_resize(src: Image, tmpl: str, similarity=0.9, step=20, fast=False): + src = cv2.cvtColor(np.asarray(src), cv2.COLOR_RGB2BGR) + src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) + tmpl = cv2.imread(tmpl) + tmpl_gray = cv2.cvtColor(tmpl, cv2.COLOR_BGR2GRAY) + found = None + tmp_r = 0 + for scale in np.linspace(0.2, 1.0, step)[::-1]: + # 根据scale比例缩放图像,并保持其宽高比 + resized = imutils.resize(src_gray, width=int(src_gray.shape[1] * scale)) + r = src_gray.shape[1] / float(resized.shape[1]) + result = cv2.matchTemplate(resized, tmpl_gray, cv2.TM_CCOEFF_NORMED) + found_location = convert_location(result, tmpl_gray, similarity, r) + # 快速匹配, 如果缩放比例越大 相似度越低就直接退出循环 返回当前最佳结果 + if fast: + if found is not None and found_location is not None: + print(found[2], found_location[2]) + if tmp_r < r and found[2] > found_location[2]: + break + tmp_r = r + # 如果我们找到了一个新的最大校正值,更新簿记变量值 + if found is None or ( + found_location is not None and found_location[2] >= similarity and found_location[2] > found[2]): + found = found_location + if found is None: + return None + x, y, _ = found + print("x,y (%s, %s)" % (x, y)) + return found[0], found[1] + + +def match(src: Image, tmpl: str, similarity=0.9): + # cv::TM_CCOEFF_NORMED = 5 标准相关匹配 + method = cv2.TM_CCOEFF_NORMED + src = cv2.cvtColor(np.asarray(src), cv2.COLOR_RGB2BGR) + # 灰度图提高精度 + src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) + tmpl = cv2.imread(tmpl) + tmpl_gray = cv2.cvtColor(tmpl, cv2.COLOR_BGR2GRAY) + # print('imagetempl.shape:', tmpl_gray.shape) + result = cv2.matchTemplate(src_gray, tmpl_gray, method) + print(result) + loc = convert_location(result, tmpl_gray, similarity) + if loc is None: + return + x, y, _ = loc + print("x,y (%s, %s)" % (x, y)) + return x, y + + +def convert_location(result, tmpl, similarity, resize=1): + method = 5 + min_max = cv2.minMaxLoc(result) + if method == 0 or method == 1: # 根据不同的模式最佳匹配位置取值方法不同 + match_loc = min_max[2] + else: + match_loc = min_max[3] + right_bottom = (match_loc[0] + tmpl.shape[1], match_loc[1] + tmpl.shape[0]) + # print('result.min_max:', min_max, 'method=', method) + # print('match_loc:', match_loc, type(match_loc)) + # print('right_bottom', right_bottom, type(right_bottom)) + x = int(match_loc[0] * resize) + int((right_bottom[0] - match_loc[0]) / 2 * resize) + y = int(match_loc[1] * resize) + int((right_bottom[1] - match_loc[1]) / 2 * resize) + + print("(%s, %s) 缩放比例: %s, 相似度: %s" % (x, y, resize, min_max[1])) + if min_max[1] < similarity: + return None + return x, y, min_max[1] diff --git a/utils/location.py b/utils/location.py new file mode 100644 index 0000000..cc18e4e --- /dev/null +++ b/utils/location.py @@ -0,0 +1,5 @@ +import pyscreeze + + +def get_location_from_box(box: pyscreeze.Box): + return box.left, box.top diff --git a/utils/matplotlib.py b/utils/matplotlib.py new file mode 100644 index 0000000..68082dc --- /dev/null +++ b/utils/matplotlib.py @@ -0,0 +1,11 @@ +from matplotlib import pyplot as plt +from matplotlib import rcParams +rcParams['font.family'] = 'SimHei' + + +def debug(image): + plt.title("测试") + plt.imshow(image) + plt.axis("off") + plt.show() +