当前位置: 首页 > news >正文

纯python实现大漠图色功能

大漠图色是一种自动化测试工具,可以用于识别屏幕上的图像并执行相应的操作。在Python中,可以使用第三方库pyautogui来实现大漠图色功能。具体步骤如下:

  1. 安装pyautogui库:在命令行中输入pip install pyautogui。
  2. 导入pyautogui库:在Python脚本中使用import pyautogui语句导入pyautogui库。
  3. 使用pyautogui.locateOnScreen()函数来查找屏幕上的图像,并返回图像的位置坐标。
  4. 使用pyautogui.click()函数来模拟鼠标点击操作。
  5. 使用pyautogui.typewrite()函数来模拟键盘输入操作。

实现如标题,废话不多说,直接看代码:

# 更新StressShow命令import numpy as np
import pyautogui
import copy
import cv2
from sklearn import clusterclass TuSe:def __init__(self):print('欢迎使用')def GetCapture(self, stax, stay, endx, endy):w = endx - staxh = endy - stayim = pyautogui.screenshot(region=(stax, stay, w, h))# im = cv2.cvtColor(np.array(im), cv2.COLOR_BGR2RGB)return np.array(im)def FindPic(self, x1, y1, x2, y2, path, thd):'''找图:param x1: 起点X:param y1: 起点Y:param x2: 终点X:param y2: 终点Y:param path: 图片路径:param thd: 相似度:return: 图片中心坐标'''img = self.GetCapture(x1, y1, x2, y2)img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)template = cv2.imread(path, 0)th, tw = template.shape[::]rv = cv2.matchTemplate(img, template, 1)minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(rv)if 1 - minVal >= thd:return minLoc[0] + tw / 2 + x1, minLoc[1] + th / 2 + y1else:return -1, -1def Hex_to_Rgb(self, hex):'''十六进制转RGB:param hex: 十六进制颜色值:return: RGB'''return np.array(tuple(int(hex[i:i + 2], 16) for i in (0, 2, 4)))def CmpColor(self, x, y, color, sim: float):'''比色:param x: X坐标:param y: Y坐标:param color: 十六进制颜色,可以从大漠直接获取:param sim: 相似偏移:return: 真或加'''img = self.GetCapture(x - 1, y - 1, x + 1, y + 1)img = np.array(img)img = img[1][1]color = self.Hex_to_Rgb(color)res = np.absolute(color - img)sim = int((1 - sim) * 255)return True if np.amax(res) <= sim else Falsedef FindColor(self, x1, y1, x2, y2, des, sim: float):'''找色:param x1: 起点X:param y1: 起点Y:param x2: 终点X:param y2: 终点Y:param des: 十六进制颜色,可以从大漠直接获取:param sim: 相似偏移:return:'''img = self.GetCapture(x1, y1, x2, y2)img = np.array(img)res = np.absolute(img - self.Hex_to_Rgb(des))sim = int((1 - sim) * 255)res = np.argwhere(np.all(res <= sim, axis=2))res = res + (y1, x1)return res[:, [1, 0]]def GetColorNum(self, x1, y1, x2, y2, des, sim: float):'''获取颜色数量:param x1: 起点X:param y1: 起点Y:param x2: 终点X:param y2: 终点Y:param des: 十六进制颜色,可以从大漠直接获取:param sim: 相似偏移:return:'''return len(self.FindColor(x1, y1, x2, y2, des, sim))def FindMultColor(self, stax, stay, endx, endy, des):'''多点找色:param stax::param stay::param endx::param endy::param des: 大漠获取到的多点找色数据,偏色必须写上:return:'''w = endx - staxh = endy - stayimg = pyautogui.screenshot(region=(stax, stay, w, h))img = np.array(img)rgby = []ps = []a = 0firstXY = []res = np.empty([0, 2])for i in des.split(','):rgb_y = i[-13:]r = int(rgb_y[0:2], 16)g = int(rgb_y[2:4], 16)b = int(rgb_y[4:6], 16)y = int(rgb_y[-2:])rgby.append([r, g, b, y])for i in range(1, len(des.split(','))):ps.append([int(des.split(',')[i].split('|')[0]), int(des.split(',')[i].split('|')[1])])for i in rgby:result = np.logical_and(abs(img[:, :, 0:1] - i[0]) < i[3], abs(img[:, :, 1:2] - i[1]) < i[3],abs(img[:, :, 2:3] - i[2]) < i[3])results = np.argwhere(np.all(result == True, axis=2)).tolist()if a == 0:firstXY = copy.deepcopy(results)else:nextnextXY = copy.deepcopy(results)for index in nextnextXY:index[0] = int(index[0]) - ps[a - 1][1]index[1] = int(index[1]) - ps[a - 1][0]q = set([tuple(t) for t in firstXY])w = set([tuple(t) for t in nextnextXY])matched = np.array(list(q.intersection(w)))res = np.append(res, matched, axis=0)a += 1unique, counts = np.unique(res, return_counts=True, axis=0)index = np.argmax(counts)re = unique[index] + (stay, stax)if np.max(counts) == len(des.split(',')) - 1:return np.flipud(re)return np.array([-1, -1])def FindPicEx(self, x1, y1, x2, y2, path, thd=0.9, MIN_MATCH_COUNT=8):'''全分辨率找图:param x1::param y1::param x2::param y2::param path::param thd: 相似度:param MIN_MATCH_COUNT: 特征点数量:return:'''thd = thd - 0.2template = cv2.imread(path, 0)  # queryImage# target = cv2.imread('target.jpg', 0)  # trainImagetarget = self.GetCapture(x1, y1, x2, y2)target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)# Initiate SIFT detector创建sift检测器sift = cv2.xfeatures2d.SIFT_create()# find the keypoints and descriptors with SIFTkp1, des1 = sift.detectAndCompute(template, None)kp2, des2 = sift.detectAndCompute(target, None)# 创建设置FLANN匹配FLANN_INDEX_KDTREE = 0index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)search_params = dict(checks=50)flann = cv2.FlannBasedMatcher(index_params, search_params)matches = flann.knnMatch(des1, des2, k=2)# store all the good matches as per Lowe's ratio test.good = []for m, n in matches:if m.distance < thd * n.distance:good.append(m)if len(good) > MIN_MATCH_COUNT:# 获取关键点的坐标src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)# 计算变换矩阵和MASKM, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)h, w = template.shape# 使用得到的变换矩阵对原图像的四个角进行变换,获得在目标图像上对应的坐标pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)dst = cv2.perspectiveTransform(pts, M)res = (dst[0] + dst[2]) / 2  # [[[ 39.11337  147.11575 ]] [[135.06624  255.12143 ]]return int(res[0][0]) + x1, int(res[0][1]) + y1else:return -1, -1def _FilterRec(self, res, loc):""" 对同一对象的多个框按位置聚类后,按置信度选最大的一个进行保留。:param res: 是 cv2.matchTemplate 返回值:param loc: 是 cv2.np.argwhere(res>threshold) 返回值:return: 返回保留的点的列表 pts"""model = cluster.AffinityPropagation(damping=0.5, max_iter=100, convergence_iter=10, preference=-50).fit(loc)y_pred = model.labels_pts = []for i in set(y_pred):argj = loc[y_pred == i]argi = argj.Tpt = argj[np.argmax(res[tuple(argi)])]pts.append(pt[::-1])return np.array(pts)def FindMultPic(self, x1, y1, x2, y2, path, thd):'''多目标找图:param x1::param y1::param x2::param y2::param path::param thd: 相似度:return:'''target = self.GetCapture(x1, y1, x2, y2)target = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)template = cv2.imread(path, 0)w, h = template.shape[:2]res = cv2.matchTemplate(target, template, cv2.TM_CCOEFF_NORMED)loc = np.argwhere(res >= thd)if len(loc):resc = self._FilterRec(res, loc)return resc + (h / 2 + x1, w / 2 + y1)else:return [[-1, -1]]def FindPic_TM(self, x1, y1, x2, y2, path, thd):'''找透明图,透明色为黑色:param x1: 起点X:param y1: 起点Y:param x2: 终点X:param y2: 终点Y:param path: 图片路径:param thd: 相似度:return: 图片中心坐标'''img = self.GetCapture(x1, y1, x2, y2)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)template = cv2.imread(path)template2 = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)ret, mask = cv2.threshold(template2, 20, 255, cv2.THRESH_BINARY)th, tw = template.shape[:2]rv = cv2.matchTemplate(img, template, 1, mask=mask)minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(rv)if 1 - minVal >= thd:return minLoc[0] + tw / 2 + x1, minLoc[1] + th / 2 + y1else:return -1, -1def StressShow(self, stax, stay, endx, endy, des, type=0):'''保留选中颜色,其他为黑色,相似度根据偏色调整:param stax::param stay::param endx::param endy::param des: 大漠的色彩描述:param type: 0为原来颜色,1为白色:return:'''# des = 'e81010-101010|f9ad08-000000'dess = des.split('|')des = [i[0:6] for i in dess]des = [np.array(self.Hex_to_Rgb(d)) for d in des]pds = [i[-6:] for i in dess]pds = tuple(tuple(int(item[i:i + 2]) for i in range(0, len(item), 2)) for item in pds)img = self.GetCapture(stax, stay, endx, endy)mask = np.zeros(img.shape[:2], dtype=np.bool_)for i, color in enumerate(des):mask += np.all(np.abs(img - color) <= pds[i], axis=-1)new_img = np.where(mask[..., None], [255, 255, 255], [0, 0, 0]) if type else np.where(mask[..., None], img,[0, 0,0])  # 修改这里,将选中的颜色设为白色img_converted = cv2.convertScaleAbs(new_img)img_converted = cv2.cvtColor(np.array(img_converted), cv2.COLOR_BGR2RGB)return img_converteda = TuSe()
b = a.StressShow(0, 0, 1920, 1080, 'e81010-101010|36659e-101010', 0)
cv2.imshow('13', b)
cv2.waitKey(0)
cv2.destroyAllWindows()

相关文章:

纯python实现大漠图色功能

大漠图色是一种自动化测试工具&#xff0c;可以用于识别屏幕上的图像并执行相应的操作。在Python中&#xff0c;可以使用第三方库pyautogui来实现大漠图色功能。具体步骤如下&#xff1a; 安装pyautogui库&#xff1a;在命令行中输入pip install pyautogui。导入pyautogui库&a…...

debounce and throtlle

debounce // 核心&#xff1a;单位时间内触发>1 则只执行最后一次。//excutioner 可以认为是执行器。执行器存在则清空&#xff0c;再赋值新的执行器。function debounce(fn, delay 500) {let excutioner null;return function () {let context this;let args arguments…...

四、数据库系统

数据库系统&#xff08;Database System&#xff09;&#xff0c;是由数据库及其管理软件组成的系统。数据库系统是为适应数据处理的需要而发展起来的一种较为理想的数据处理系统&#xff0c;也是一个为实际可运行的存储、维护和应用系统提供数据的软件系统&#xff0c;是存储介…...

Linux中的高级IO

文章目录 1.IO1.1基本介绍1.2基础io的低效性1.3如何提高IO效率1.4五种IO模型1.5非阻塞模式的设置 2.IO多路转接之Select2.1函数的基本了解2.2fd_set理解2.3完整例子代码&#xff08;会在代码中进行讲解&#xff09;2.4优缺点 3.多路转接之poll3.1poll函数的介绍3.2poll服务器3.…...

项目管理之如何估算项目工作成本

在项目管理中&#xff0c;如何估算项目工作成本是一个关键问题。为了解决这个问题&#xff0c;我们可以采用自上而下的成本限额估算法和自下而上的成本汇总估算法。这两种方法各有优缺点&#xff0c;但都可以帮助我们准确地估算项目工作成本。 自上而下的成本限额估算法 自上…...

Redis主从复制基础概念

Redis主从复制&#xff1a;提高数据可用性和性能的策略 一、概述 Redis主从复制是一种常用的高可用性策略&#xff0c;通过将数据从一个Redis服务器复制到另一个或多个Redis服务器上&#xff0c;以提高数据的可用性和读取性能。当主服务器出现故障时&#xff0c;可以快速地切…...

图数据库Neo4j概念、应用场景、安装及CQL的使用

一、图数据库概念 引用Seth Godin的说法&#xff0c;企业需要摒弃仅仅收集数据点的做法&#xff0c;开始着手建立数据之间的关联关系。数据点之间的关系甚至比单个点本身更为重要。 传统的**关系数据库管理系统(RDBMS)**并不擅长处理数据之间的关系&#xff0c;那些表状数据模…...

路由器基础(四): RIP原理与配置

路由信息协议 (Routing Information Protocol,RIP) 是最早使用的距离矢量路由协议。因为路由是以矢量(距离、方向)的方式被通告出去的&#xff0c;这里的距离是根据度量来决定的&#xff0c;所以叫“距离矢量”。 距离矢量路由算法是动态路由算法。它的工作流程是&#xff1a;…...

红外遥控开发RK3568-PWM-IR

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言1.红外遥控的发送接收工作原理2.红外协议3.红外遥控系统框图4.遥控器添加方法4.1 记录键值4.2 添加键值总结前言 提示:这里可以添加本文要记录的大概内容: 1.红外遥控的发送接收工作原理 …...

go-sync-mutex

Sync ​ Go 语言作为一个原生支持用户态进程&#xff08;Goroutine&#xff09;的语言&#xff0c;当提到并发编程、多线程编程时&#xff0c;往往都离不开锁这一概念。锁是一种并发编程中的同步原语&#xff08;Synchronization Primitives&#xff09;&#xff0c;它能保证多…...

高并发系统设计

高并发系统通用设计方法 Scala-out 横向扩展&#xff0c;分散流量&#xff0c;分布式集群部署 缺点&#xff1a;引入复杂度&#xff0c;节点之间状态维护&#xff0c;节点扩展&#xff08;上下线&#xff09; Scala-up 提升单机性能&#xff0c;比如增加内存&#xff0c;增…...

Vue3-Pinia快速入门

1.安装pinia npm install pinia -save 2.在main.js中导入并使用pinia // 导入piniaimport { createPinia } from "pinia"; const pinia createPinia();//使用pinia app.use(pinia)app.mount(#app) 3.在src目录下创建包&#xff1a;store&#xff0c;表示仓库 4…...

Python算法——插入排序

插入排序&#xff08;Insertion Sort&#xff09;是一种简单但有效的排序算法&#xff0c;它的基本思想是将数组分成已排序和未排序两部分&#xff0c;然后逐一将未排序部分的元素插入到已排序部分的正确位置。插入排序通常比冒泡排序和选择排序更高效&#xff0c;特别适用于对…...

Java21新特性

目录 一、Java21新特性 1、字符串模版 2、scoped values 3、record pattern 4、switch格式匹配 5、可以在switch中使用when 6、Unnamed Classes and Instance Main Methods 7、Structured Concurrency 一、Java21新特性 1、字符串模版 字符串模版可以让开发者更简洁的…...

4 Tensorflow图像识别模型——数据预处理

上一篇&#xff1a;3 tensorflow构建模型详解-CSDN博客 本篇开始介绍识别猫狗图片的模型&#xff0c;内容较多&#xff0c;会分为多个章节介绍。模型构建还是和之前一样的流程&#xff1a; 数据集准备数据预处理创建模型设置损失函数和优化器训练模型 本篇先介绍数据集准备&am…...

SpringBoot整合RabbitMQ学习笔记

SpringBoot整合RabbitMQ学习笔记 以下三种类型的消息&#xff0c;生产者和消费者需各自启动一个服务&#xff0c;模拟生产者服务发送消息&#xff0c;消费者服务监听消息&#xff0c;分布式开发。 一 Fanout类型信息 . RabbitMQ创建交换机和队列 在RabbitMQ控制台&#xff0c;新…...

在校园跑腿系统小程序中,如何设计高效的实时通知与消息推送系统?

1. 选择合适的消息推送服务 在校园跑腿系统小程序中&#xff0c;选择一个适合的消息推送服务。例如&#xff0c;使用WebSocket技术、Firebase Cloud Messaging (FCM)、或第三方推送服务如Pusher或OneSignal等。注册并获取相关的API密钥或访问令牌。 2. 集成服务到小程序后端…...

求极限Lim x->0 (x-sinx)*e-²x / (1-x)⅓

题目如下&#xff1a; 解题思路: 这题运用了无穷小替换、洛必达法则、求导法则 具体解题思路如下: 1、首先带入x趋近于0&#xff0c;可以得到&#xff08;0*1&#xff09;/0&#xff0c;所以可以把e的-x的平方沈略掉 然后根据无穷小替换&#xff0c;利用t趋近于0时&#xf…...

JavaScript数据类型详细解析与代码实例

JavaScript是一种弱类型动态语言&#xff0c;数据类型分为原始类型和对象类型。 原始类型 原始类型包括&#xff1a;数字、字符串、布尔值和undefined、null。 数字 JavaScript中的数字类型包括整数和浮点数&#xff0c;可以进行基本的数学运算。 var num1 10; // 整数 v…...

.NET Framework中自带的泛型委托Func

Func<>是.NET Framework中自带的泛型委托&#xff0c;可以接收一个或多个输入参数&#xff0c;并且有返回值&#xff0c;和Action类似&#xff0c;.NET基类库也提供了多达16个输入参数的Func委托&#xff0c;输出参数只有1个。 1、Func泛型委托 .NET Framework为我们提…...

Python爬虫实战:研究MechanicalSoup库相关技术

一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

conda相比python好处

Conda 作为 Python 的环境和包管理工具&#xff0c;相比原生 Python 生态&#xff08;如 pip 虚拟环境&#xff09;有许多独特优势&#xff0c;尤其在多项目管理、依赖处理和跨平台兼容性等方面表现更优。以下是 Conda 的核心好处&#xff1a; 一、一站式环境管理&#xff1a…...

C++初阶-list的底层

目录 1.std::list实现的所有代码 2.list的简单介绍 2.1实现list的类 2.2_list_iterator的实现 2.2.1_list_iterator实现的原因和好处 2.2.2_list_iterator实现 2.3_list_node的实现 2.3.1. 避免递归的模板依赖 2.3.2. 内存布局一致性 2.3.3. 类型安全的替代方案 2.3.…...

23-Oracle 23 ai 区块链表(Blockchain Table)

小伙伴有没有在金融强合规的领域中遇见&#xff0c;必须要保持数据不可变&#xff0c;管理员都无法修改和留痕的要求。比如医疗的电子病历中&#xff0c;影像检查检验结果不可篡改行的&#xff0c;药品追溯过程中数据只可插入无法删除的特性需求&#xff1b;登录日志、修改日志…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

2021-03-15 iview一些问题

1.iview 在使用tree组件时&#xff0c;发现没有set类的方法&#xff0c;只有get&#xff0c;那么要改变tree值&#xff0c;只能遍历treeData&#xff0c;递归修改treeData的checked&#xff0c;发现无法更改&#xff0c;原因在于check模式下&#xff0c;子元素的勾选状态跟父节…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验

系列回顾&#xff1a; 在上一篇中&#xff0c;我们成功地为应用集成了数据库&#xff0c;并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了&#xff01;但是&#xff0c;如果你仔细审视那些 API&#xff0c;会发现它们还很“粗糙”&#xff1a;有…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

【分享】推荐一些办公小工具

1、PDF 在线转换 https://smallpdf.com/cn/pdf-tools 推荐理由&#xff1a;大部分的转换软件需要收费&#xff0c;要么功能不齐全&#xff0c;而开会员又用不了几次浪费钱&#xff0c;借用别人的又不安全。 这个网站它不需要登录或下载安装。而且提供的免费功能就能满足日常…...