纯python实现大漠图色功能

2023-11-03 07:20

本文主要是介绍纯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实现大漠图色功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/336514

相关文章

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

【Python编程】Linux创建虚拟环境并配置与notebook相连接

1.创建 使用 venv 创建虚拟环境。例如,在当前目录下创建一个名为 myenv 的虚拟环境: python3 -m venv myenv 2.激活 激活虚拟环境使其成为当前终端会话的活动环境。运行: source myenv/bin/activate 3.与notebook连接 在虚拟环境中,使用 pip 安装 Jupyter 和 ipykernel: pip instal

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss