python 不用opencv和aircv解决图片在屏幕中的位置

from PIL import Image, ImageFile

def get_position ():
    image_path = os.path.abspath(__file__)
    image_path = image_path + "\\resource\\images\\logo.png"
    position = search_position(image_path)
    if position is None:
        return
    MAIN_X = position[0]
    MAIN_Y = position[1]


def search_position(file_name):
    im1 = pyautogui.screenshot()
    im2 = Image.open(file_name)
    pix1 = im1.load()
    pix2 = im2.load()

    width1 = im1.size[0]
    height1 = im1.size[1]
    width2 = im2.size[0]
    height2 = im2.size[1]

    threshold = 5  # 设置阈值
    rgb2 = pix2[0, 0][:3] 
    for x in range(width1 - width2):
        for y in range(height1 - height2):
            rgb1 = pix1[x, y][:3]
            if rgb1 == rgb2:
                status = 0
                for s in range(x, x + width2, 2):  # 每隔一个点检查一次
                    for j in range(y, y + height2, 2):
                        if abs(pix2[s - x, j - y][0] - pix1[s, j][0]) > threshold and abs(
                                pix2[s - x, j - y][1] - pix1[s, j][:3][1]) > threshold and abs(
                                    pix2[s - x, j - y][2] - pix1[s, j][:3][2]) > threshold:
                            status = 1
                if status:
                    continue
                else:
                    position = [x, y]
                    return position
    return None
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值