当前位置:   article > 正文

python实现按键精灵之找图点击_python 找图

python 找图
dm.click(r'C:\Users\999\Desktop\123.bmp')#找图点击
dm.doubleClick('1.jpg')#找图双击
dm.locateOnScreen('1.bmp', region=(0,0,0,0))#区域找图
img = dm.locateOnScreen('img.png')#全屏找图
dm.click('button.png')#找图点击1
x, y = dm.locateCenterOnScreen('1.bmp')#找图点击2
dm.click(x, y)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Version  : 3.8
# @Author   : QQ736592720
# @Datetime : 2023/1/12 16:33
# @Project  : smf
# @File     : my_pic.py
import cv2

from ctypes import *  # 获取屏幕上某个坐标的颜色
from PIL import ImageGrab
import numpy as np
from rgb_hex import *
import os.path
import aircv, cv2


def GetPixelColor(x, y):
    '''
    获取指定点的颜色 十六进制FFFFFF str
    :param x: 指定点的横向坐标 int
    :param y: 指定点的纵向坐标 int
    :return: 十六进制FFFFFF str
    '''
    gdi32 = windll.gdi32
    user32 = windll.user32
    hdc = user32.GetDC(None)  # 获取颜色值
    pixel = gdi32.GetPixel(hdc, x, y)  # 提取RGB值
    r = pixel & 0x0000ff
    g = (pixel & 0x00ff00) >> 8
    b = pixel >> 16
    return rgb2hex((r, g, b)).upper()


def pil2np(pil_img):
    '''
    PIL.Image.Image===>numpy.ndarray
    :param pil_img: pil模块的图片数据  PIL.Image.Image
    :return: numpy 支持的narray 数据结构   numpy.ndarray
    '''
    print(type(pil_img))  # PIL.Image.Image
    return np.array(pil_img)  # numpy.ndarray


def FindColor(x0, y0, x1, y1, color_hex):
    '''
    0,0,1024,768,"0000FF"
    :param x0: 找色区域左上角x坐标 int
    :param y0: 找色区域左上角y坐标 int
    :param x1: 找色区域右下角x坐标 int
    :param y1: 找色区域右下角y坐标 int
    :param color_hex:"0000FF"
    :return: (0,100) |(-1,-1)
    '''

    color = hex2rgb(color_hex)
    img = pil2np(ImageGrab.grab((x0, y0, x1, y1)))
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if img[i, j, 0] == color[0] and img[i, j, 1] == color[1] and img[i, j, 2] == color[2]:
                return i, j
    print("没找到point")
    return -1, -1


# 0,0,1024,768,"Attachment:\神盾.bmp",0.9
def FindPic(x0, y0, x1, y1, small_picture_path, similarity=0.95):
    '''
    区域找图
    :param x0: 找色区域左上角x坐标 int
    :param y0: 找色区域左上角y坐标 int
    :param x1: 找色区域右下角x坐标 int
    :param y1: 找色区域右下角y坐标 int
    :param small_picture_path:目标小图的路径
    :param similarity:相似度,0.95
    :return: (0,100) |(-1,-1)
    '''
    if not os.path.exists(small_picture_path):
        return None
    bag = ImageGrab.grab((x0, y0, x1, y1))
    small = cv2.imread(small_picture_path)
    res = aircv.find_template(pil2np(bag), small, similarity)
    # {'result': (430.0, 30.5), 'rectangle': ((420, 19), (420, 42), (440, 19), (440, 42)), 'confidence': 1.0}
    return res["result"] if res else (-1, -1)


if __name__ == '__main__':
    from my_mouse import *
    r = ImageGrab.grab((0, 0, 582, 175))
    r = pil2np(r)
    print(type(r))
    # r = FindPic(0, 0, 1000, 1000, r"C:\Users\Administrator.USER-20200912KS\Desktop\small.bmp")
    # print(r)
    # LeftClick(*r)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/787591
推荐阅读
相关标签
  

闽ICP备14008679号