简单试水

This commit is contained in:
shikong 2023-09-14 02:40:30 +08:00
commit e949d13f0b
16 changed files with 176 additions and 0 deletions

10
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Zeppelin 忽略的文件
/ZeppelinRemoteNotebooks/

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="Python 3.10" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/py-autogui.iml" filepath="$PROJECT_DIR$/.idea/py-autogui.iml" />
</modules>
</component>
</project>

9
.idea/py-autogui.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

BIN
assets/images/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

BIN
assets/images/win_win10.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

0
func/__init__.py Normal file
View File

12
func/location.py Normal file
View File

@ -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)

33
main.py Normal file
View File

@ -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)

BIN
snap/grab0.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 753 KiB

BIN
snap/grab1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 227 KiB

0
utils/__init__.py Normal file
View File

76
utils/cv2.py Normal file
View File

@ -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]

5
utils/location.py Normal file
View File

@ -0,0 +1,5 @@
import pyscreeze
def get_location_from_box(box: pyscreeze.Box):
return box.left, box.top

11
utils/matplotlib.py Normal file
View File

@ -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()