import pyautogui
import cv2
import numpy as np
import time"""
小型自动化工具
图片识别
自动化简单工具
"""# -*- coding:utf-8 -*-
# #cv读取中文有点问题 需要修改库里使用编码为utf-8
# 获取图像数据匹配全屏寻找
class Mouse():def __init__(self, address): # 创建Mouse的时候加入图片地址参数time.sleep(0.5)self.address = addressimg = pyautogui.screenshot(region=[0, 0, pyautogui.size().width, pyautogui.size().height]) # x,y,w,himg = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # cvtColor用于在图像中不同的色彩空间进行转换,用于后续处理。template = cv2.imread(self.address, cv2.COLOR_RGB2BGR)res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)self.max_loc = max_locself.template = templateself.X = max_loc[0] + template.shape[1] / 2self.Y = max_loc[1] + template.shape[0] / 2self.max_val = max_valself.img = img# 移动鼠标def move(self, val=0.85, x=0, y=0): # x,y为偏移量if self.max_val > val:pyautogui.moveTo(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 获取匹配度def value(self):print("匹配度:", self.max_val)# 进行点击def click(self, val=0.85, x=0, y=0): # x,y为偏移量if self.max_val > val:pyautogui.click(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 进行双击def doubleClick(self, val=0.85, x=0, y=0): # x,y为偏移量""":param val: 匹配度:param x: x轴偏移 左负右正:param y: y轴偏移 上加下减:return:"""if self.max_val > val:pyautogui.doubleClick(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 进行显示def show(self, val=0.85):if self.max_val > val:right_bottom = (self.max_loc[0] + self.template.shape[1], self.max_loc[1] + self.template.shape[0]) # 右下角cv2.rectangle(self.img, self.max_loc, right_bottom, 255, 2) # 画出矩形位置cv2.imshow("output", self.img)cv2.waitKey(0)# 写入
def write(string):pyautogui.write(string)# 删除
def delete():pyautogui.hotkey('Ctrl', 'a')pyautogui.press('delete')# 点击
def click(x, y, duration=0.2):pyautogui.click(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 移动
def move(x, y, duration=0.2):pyautogui.moveTo(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 右键
def clickRight(x, y, duration=0.2):pyautogui.rightClick(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 操作流程
m = Mouse("test.jpg")
m.move()
m.value()
m.show()
python">import pyautogui
import cv2
import numpy as np
import time"""
小型自动化工具
图片识别
自动化简单工具
"""# -*- coding:utf-8 -*-
# #cv读取中文有点问题 需要修改库里使用编码为utf-8
# 获取图像数据匹配全屏寻找
class Mouse():def __init__(self, address): # 创建Mouse的时候加入图片地址参数time.sleep(0.5)self.address = addressimg = pyautogui.screenshot(region=[0, 0, pyautogui.size().width, pyautogui.size().height]) # x,y,w,himg = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # cvtColor用于在图像中不同的色彩空间进行转换,用于后续处理。template = cv2.imread(self.address, cv2.COLOR_RGB2BGR)res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)self.max_loc = max_locself.template = templateself.X = max_loc[0] + template.shape[1] / 2self.Y = max_loc[1] + template.shape[0] / 2self.max_val = max_valself.img = img# 移动鼠标def move(self, val=0.85, x=0, y=0): # x,y为偏移量if self.max_val > val:pyautogui.moveTo(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 获取匹配度def value(self):print("匹配度:", self.max_val)# 进行点击def click(self, val=0.85, x=0, y=0): # x,y为偏移量if self.max_val > val:pyautogui.click(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 进行双击def doubleClick(self, val=0.85, x=0, y=0): # x,y为偏移量""":param val: 匹配度:param x: x轴偏移 左负右正:param y: y轴偏移 上加下减:return:"""if self.max_val > val:pyautogui.doubleClick(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)# 进行显示def show(self, val=0.85):if self.max_val > val:right_bottom = (self.max_loc[0] + self.template.shape[1], self.max_loc[1] + self.template.shape[0]) # 右下角cv2.rectangle(self.img, self.max_loc, right_bottom, 255, 2) # 画出矩形位置cv2.imshow("output", self.img)cv2.waitKey(0)# 写入
def write(string):pyautogui.write(string)# 删除
def delete():pyautogui.hotkey('Ctrl', 'a')pyautogui.press('delete')# 点击
def click(x, y, duration=0.2):pyautogui.click(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 移动
def move(x, y, duration=0.2):pyautogui.moveTo(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 右键
def clickRight(x, y, duration=0.2):pyautogui.rightClick(x, y, duration=duration, tween=pyautogui.easeInOutQuad)# 操作流程
m = Mouse("test.jpg")
m.move()
m.value()
m.show()
# -*- coding:utf-8 -*-
import pyautogui
import cv2
from pynput.keyboard import Key, Listener as Listener_K, Controller as Controller_K
from pynput.mouse import Button, Listener as Listener_M, Controller as Controller_M
import numpy as np'''
通过按键来触发
可以容错,不固定方位
如果有问题输出错误
如果没问题就输出两点的坐标
需求:
1、按键触发
2、输出大致图片
3、检测错误
'''run_key = Key.ctrl_l # 按键触发,ctrl_l即左边的Ctrl键
coordinate = [] # 存储坐标x1,y1,x2,y2def on_click(x, y, button, pressed): # 监听鼠标if pressed: # 按压打印print(f'{button} pressed at {x}, {y}')coordinate.extend([x, y])else:return Falsedef on_release(key):'释放按键时执行。'print(key)if key == run_key: # 可以改str类型来判断输入字母之类的return False # 停止监听,作为运行if __name__ == '__main__':keyboard = Controller_K() # 监听按键with Listener_K(on_release=on_release) as listener:listener.join()# 监听到特定按键跳转print("鼠标点击")mouse = Controller_M() # 监听鼠标for _ in range(2):with Listener_M(on_click=on_click) as listener:listener.join()print(coordinate)# 如果没有特殊情况,就截图if coordinate[0] - coordinate[2] == 0 or coordinate[1] - coordinate[3] == 0:print("错误")else:# 获取左上角坐标和差值coord, distance = [], []for n in range(2):if coordinate[n] < coordinate[n + 2]:coord.append(coordinate[n])else:coord.append(coordinate[n + 2])distance.append(abs(coordinate[n] - coordinate[n + 2]))img = pyautogui.screenshot(region=[coord[0], coord[1], distance[0], distance[1]]) # 左上角坐标和宽、高img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)name = input("输入保存名称:")cv2.imwrite(name+".jpg", img)
python"># -*- coding:utf-8 -*-
import pyautogui
import cv2
from pynput.keyboard import Key, Listener as Listener_K, Controller as Controller_K
from pynput.mouse import Button, Listener as Listener_M, Controller as Controller_M
import numpy as np'''
通过按键来触发
可以容错,不固定方位
如果有问题输出错误
如果没问题就输出两点的坐标
需求:
1、按键触发
2、输出大致图片
3、检测错误
'''run_key = Key.ctrl_l # 按键触发,ctrl_l即左边的Ctrl键
coordinate = [] # 存储坐标x1,y1,x2,y2def on_click(x, y, button, pressed): # 监听鼠标if pressed: # 按压打印print(f'{button} pressed at {x}, {y}')coordinate.extend([x, y])else:return Falsedef on_release(key):'释放按键时执行。'print(key)if key == run_key: # 可以改str类型来判断输入字母之类的return False # 停止监听,作为运行if __name__ == '__main__':keyboard = Controller_K() # 监听按键with Listener_K(on_release=on_release) as listener:listener.join()# 监听到特定按键跳转print("鼠标点击")mouse = Controller_M() # 监听鼠标for _ in range(2):with Listener_M(on_click=on_click) as listener:listener.join()print(coordinate)# 如果没有特殊情况,就截图if coordinate[0] - coordinate[2] == 0 or coordinate[1] - coordinate[3] == 0:print("错误")else:# 获取左上角坐标和差值coord, distance = [], []for n in range(2):if coordinate[n] < coordinate[n + 2]:coord.append(coordinate[n])else:coord.append(coordinate[n + 2])distance.append(abs(coordinate[n] - coordinate[n + 2]))img = pyautogui.screenshot(region=[coord[0], coord[1], distance[0], distance[1]]) # 左上角坐标和宽、高img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)name = input("输入保存名称:")cv2.imwrite(name+".jpg", img)