python根据截图坐标识别文字

import ddddocr
import pyautogui
import io


def capture_image(left, top, width, height):
    # 截取屏幕图像并返回图像对象
    return pyautogui.screenshot(region=(left, top, width, height))


def ocr_text(left, top, width, height, expected_text):
    # 截取图片并识别文字
    image = capture_image(left, top, width, height)

    # 使用BytesIO将图像保存为字节流
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='PNG')  # 保存为PNG格式
    img_byte_arr.seek(0)  # 重置流位置

    ocr = ddddocr.DdddOcr()

    # 进行OCR识别
    result = ocr.classification(img_byte_arr.read())
    print(f'自动识别为:{result}')

    # 返回识别结果与预期文本的比较
    return result == expected_text


if __name__ == '__main__':
    # 已知的右下角坐标和矩形的宽高
    bottom_right_x = 59
    bottom_right_y = 115
    width = 59
    height = 29

    # 计算左上角坐标
    top_left_x = bottom_right_x - width
    top_left_y = bottom_right_y - height

    # 打印结果
    print("Bounding Rectangle:")
    print("左上角坐标: ({}, {})".format(top_left_x, top_left_y))
    print("右下角坐标: ({}, {})".format(bottom_right_x, bottom_right_y))

    # 使用 PIL 显示截取的图像
    screenshot = capture_image(top_left_x, top_left_y, width, height)
    # screenshot.save("screenshot.png")  # 保存截取的图像
    screenshot.show()  # 显示截取的图像

    # 调用OCR函数进行识别
    result = ocr_text(top_left_x, top_left_y, width, height, 'aa')
    print(f'识别是否成功:{result}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值