吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2360|回复: 2
收起左侧

[Python 转载] 图色基础功能

[复制链接]
18834161486 发表于 2022-3-29 20:35
先将代码实例化运行一次(参数只能传一级窗口的类名),会把窗口置于左上角位置。然后用图色工具获取范围颜色等值,推荐大漠综合工具。最后面给了一个多点找色的例子
[Python] 纯文本查看 复制代码
import time
import win32gui, win32ui, win32con
import ctypes
import pyautogui
import numpy as np
import copy


class GameAutoGui:
    def __init__(self, className):
        print('欢迎使用游戏自动化类库')
        self.hwnd = self.FindWin(className)
        win32gui.SetForegroundWindow(self.hwnd)
        # self.l, self.t, self.r, self.b = self.get_window_rect(self.hwnd)
        self.setWinPos(self.hwnd, 0, 0)
        # print(self.hwnd, self.l, self.t, self.r, self.b)

    def FindWin(self, des):
        return win32gui.FindWindow(des, None)

    def get_appoint_hwnd(self, hwnd: str, num: int):
        '''
        获取所有指定句柄
        :param hwnd: 类名或者窗口名字
        :param num: 0对应类名,1对应窗口名字
        :return: 数组
        '''
        hwnd_title = dict()
        li = []

        def get_all_hwnd(hwnd, mouse):
            if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
                if num:
                    hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
                else:
                    hwnd_title.update({hwnd: win32gui.GetClassName(hwnd)})

        win32gui.EnumWindows(get_all_hwnd, 0)
        for h, t in hwnd_title.items():
            if str(t) == hwnd:
                # print(h, t)
                li.append(h)
        return li

    def get_window_rect(self, hwnd):
        '''
        获取窗口坐标
        :param hwnd: 句柄所在指针
        :return: 元组(rect.left, rect.top, rect.right, rect.bottom)
        '''
        import ctypes.wintypes
        try:
            f = ctypes.windll.dwmapi.DwmGetWindowAttribute
        except WindowsError:
            f = None
        if f:
            rect = ctypes.wintypes.RECT()
            DWMWA_EXTENDED_FRAME_BOUNDS = 9
            f(ctypes.wintypes.HWND(hwnd),
              ctypes.wintypes.DWORD(DWMWA_EXTENDED_FRAME_BOUNDS),
              ctypes.byref(rect),
              ctypes.sizeof(rect)
              )
            return rect.left, rect.top, rect.right, rect.bottom

    def click(self, x, y):
        x = int(x)
        y = int(y)
        ctypes.windll.user32.SetCursorPos(x, y)
        ctypes.windll.user32.mouse_event(2, 0, 0, 0, 0)
        time.sleep(0.1)
        ctypes.windll.user32.mouse_event(4, 0, 0, 0, 0)

    def setWinPos(self, hwnd, x, y):
        '''
        设置窗口位置
        :param hwnd: 句柄
        :param x: X坐标
        :param y: Y坐标
        :return:
        '''
        return ctypes.windll.user32.SetWindowPos(hwnd, 0, x, y, 0, 0, 0X0001 | 0X0004)

    #  截图
    def getCapture(self, stax, stay, endx, endy):
        w = endx - stax
        h = endy - stay
        # print(stax, stay, endx, endy,w,h)
        im = pyautogui.screenshot(region=(stax, stay, w, h))
        im.save("123.bmp")

    # 定点比色
    def cmpcolor(self, x, y, color):
        # print(x, y)
        color = self.Hex_to_RGB2(color)
        im = pyautogui.screenshot(region=(0, 0, x + 10, y + 10))
        res = im.getpixel((x, y))
        # print(res)
        if res[0] == color[0] and res[1] == color[1] and res[2] == color[2]:
            return True
        else:
            return False

    def RGB_to_Hex(self, rgb):
        RGB = rgb.split(',')  # 将RGB格式划分开来
        color = '#'
        for i in RGB:
            num = int(i)
            # 将R、G、B分别转化为16进制拼接转换并大写  hex() 函数用于将10进制整数转换成16进制,以字符串形式表示
            color += str(hex(num))[-2:].replace('x', '0').upper()
        print(color)
        return color

    def Hex_to_RGB(self, hex):
        r = int(hex[1:3], 16)
        g = int(hex[3:5], 16)
        b = int(hex[5:7], 16)
        return b, g, r

    def Hex_to_RGB2(self, hex):
        r = int(hex[1:3], 16)
        g = int(hex[3:5], 16)
        b = int(hex[5:7], 16)
        return r, g, b

    #  获取颜色数量
    def getcolornum(self, stax: int, stay: int, endx: int, endy: int, des):
        w = endx - stax
        h = endy - stay
        im = pyautogui.screenshot(region=(stax, stay, w, h))
        im = np.array(im)
        # print(im)
        des = list(self.Hex_to_RGB2(des))
        # print(des)
        result = np.count_nonzero(np.all(im == des, axis=2))
        print(result)
        return result

    #  找色
    def FindColor(self, stax: int, stay: int, endx: int, endy: int, des):
        w = endx - stax
        h = endy - stay
        im = pyautogui.screenshot(region=(stax, stay, w, h))
        im = np.array(im)
        des = list(self.Hex_to_RGB2(des))
        result = np.argwhere(np.all(im == des, axis=2)).tolist()
        print(result)
        for i in result:
            a = i[0]
            i[0] = stax + i[1]
            i[1] = stay + a
        # print(result)
        return result

    # 多点找色
    def FindFindColor(self, stax: int, stay: int, endx: int, endy: int, des):
        w = endx - stax
        h = endy - stay
        im = pyautogui.screenshot(region=(stax, stay, w, h))
        im = np.array(im)
        global headCoordinate, matched, corrdinate
        corrdinate = np.empty([0, 2])
        for i in des:
            if type(i) == str:
                # print('我是头部', i)
                headColor = self.Hex_to_RGB2(i)
                # print('头部颜色', headColor)
                headCoordinate = np.argwhere(np.all(im == headColor, axis=2))
                # print('头部坐标', headCoordinate)
            else:
                # print('我是身体', i)
                bodyColor = self.Hex_to_RGB2(i[2])
                # print('身体颜色', bodyColor)
                bodyCoordinate = np.argwhere(np.all(im == bodyColor, axis=2))
                # print('身体坐标', bodyCoordinate)
                headBodyCoordinate = copy.deepcopy(headCoordinate)
                for index in headBodyCoordinate:
                    index[0] = int(index[0]) + i[1]
                    index[1] = int(index[1]) + i[0]
                # print('头部坐标', headCoordinate)
                # print('头部偏移后坐标', headBodyCoordinate)
                q = set([tuple(t) for t in bodyCoordinate])
                w = set([tuple(t) for t in headBodyCoordinate])
                matched = np.array(list(q.intersection(w)))
                # print('相同坐标', matched)
                beforeMatched = copy.deepcopy(matched)
                for index in beforeMatched:
                    index[0] = int(index[0]) - i[1]
                    index[1] = int(index[1]) - i[0]
                # print('相同坐标的原坐标', beforeMatched)
                corrdinate = np.append(corrdinate, beforeMatched, axis=0)
                # print('中间结果', corrdinate)
        # print('结果', corrdinate)
        corrdinate = corrdinate.tolist()
        for i in corrdinate:
            # print(corrdinate.count(i))
            if corrdinate.count(i) == len(des) - 1:
                return i[1] + stax, i[0] + stay
        return -1,-1


# a = GameAutoGui('LDPlayerMainFrame')
# b = a.FindFindColor(20,361,117,467,
#                     ['#a3906f', [29, 35, '#22a0e5'], [35, 41, '#d82c29'], [25, 3, '#887b65'], [14, 6, '#3c3734']])
# print(b)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

daimmon 发表于 2022-3-30 07:47
[Python] 纯文本查看 复制代码
import pyautogui
import time
import xlrd
import pyperclip

#定义鼠标事件

#pyautogui库其他用法 https://blog.csdn.net/qingfengxd1/article/details/108270159

def mouseClick(clickTimes,lOrR,img,reTry):
    if reTry == 1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                break
            print("未找到匹配图片,0.1秒后重试")
            time.sleep(0.1)
    elif reTry == -1:
        while True:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            time.sleep(0.1)
    elif reTry > 1:
        i = 1
        while i < reTry + 1:
            location=pyautogui.locateCenterOnScreen(img,confidence=0.9)
            if location is not None:
                pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
                print("重复")
                i += 1
            time.sleep(0.1)




# 数据检查
# cmdType.value  1.0 左键单击    2.0 左键双击  3.0 右键单击  4.0 输入  5.0 等待  6.0 滚轮
# ctype     空:0
#           字符串:1
#           数字:2
#           日期:3
#           布尔:4
#           error:5
def dataCheck(sheet1):
    checkCmd = True
    #行数检查
    if sheet1.nrows<2:
        print("没数据啊哥")
        checkCmd = False
    #每行数据检查
    i = 1
    while i < sheet1.nrows:
        # 第1列 操作类型检查
        cmdType = sheet1.row(i)[0]
        if cmdType.ctype != 2 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0 
        and cmdType.value != 4.0 and cmdType.value != 5.0 and cmdType.value != 6.0):
            print('第',i+1,"行,第1列数据有毛病")
            checkCmd = False
        # 第2列 内容检查
        cmdValue = sheet1.row(i)[1]
        # 读图点击类型指令,内容必须为字符串类型
        if cmdType.value ==1.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
            if cmdValue.ctype != 1:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 输入类型,内容不能为空
        if cmdType.value == 4.0:
            if cmdValue.ctype == 0:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 等待类型,内容必须为数字
        if cmdType.value == 5.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        # 滚轮事件,内容必须为数字
        if cmdType.value == 6.0:
            if cmdValue.ctype != 2:
                print('第',i+1,"行,第2列数据有毛病")
                checkCmd = False
        i += 1
    return checkCmd

#任务
def mainWork(img):
    i = 1
    while i < sheet1.nrows:
        #取本行指令的操作类型
        cmdType = sheet1.row(i)[0]
        if cmdType.value == 1.0:
            #取图片名称
            img = sheet1.row(i)[1].value
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"left",img,reTry)
            print("单击左键",img)
        #2代表双击左键
        elif cmdType.value == 2.0:
            #取图片名称
            img = sheet1.row(i)[1].value
            #取重试次数
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(2,"left",img,reTry)
            print("双击左键",img)
        #3代表右键
        elif cmdType.value == 3.0:
            #取图片名称
            img = sheet1.row(i)[1].value
            #取重试次数
            reTry = 1
            if sheet1.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
                reTry = sheet1.row(i)[2].value
            mouseClick(1,"right",img,reTry)
            print("右键",img) 
        #4代表输入
        elif cmdType.value == 4.0:
            inputValue = sheet1.row(i)[1].value
            pyperclip.copy(inputValue)
            pyautogui.hotkey('ctrl','v')
            time.sleep(0.5)
            print("输入:",inputValue)                                        
        #5代表等待
        elif cmdType.value == 5.0:
            #取图片名称
            waitTime = sheet1.row(i)[1].value
            time.sleep(waitTime)
            print("等待",waitTime,"秒")
        #6代表滚轮
        elif cmdType.value == 6.0:
            #取图片名称
            scroll = sheet1.row(i)[1].value
            pyautogui.scroll(int(scroll))
            print("滚轮滑动",int(scroll),"距离")                      
        i += 1

if __name__ == '__main__':
    file = 'cmd.xls'
    #打开文件
    wb = xlrd.open_workbook(filename=file)
    #通过索引获取表格sheet页
    sheet1 = wb.sheet_by_index(0)
    print('欢迎使用不高兴就喝水牌RPA~')
    #数据检查
    checkCmd = dataCheck(sheet1)
    if checkCmd:
        key=input('选择功能: 1.做一次 2.循环到死 \n')
        if key=='1':
            #循环拿出每一行指令
            mainWork(sheet1)
        elif key=='2':
            while True:
                mainWork(sheet1)
                time.sleep(0.1)
                print("等待0.1秒")    
    else:
        print('输入有误或者已经退出!')
头像被屏蔽
请叫我老龚 发表于 2022-3-30 16:26
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 07:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表