python截图工具

#截图工具+ocr光学识别案例(初稿)

# 1、获取开始点坐标和结束点坐标确定截取屏幕的大小

# 2、屏幕截取

# 3、ocr光学识别

# 4、输出结果

import pyautogui    #自动化库

import time     #时间库

from cnocr import CnOcr

# 获取坐标

def get_coordinates():

    # 在开始坐标位置停顿4秒钟,取该点为开始点坐标

    # 在开始坐标位置停顿4秒钟,取该点为结束点坐标

    # cooradinates的英文单词为坐标

    coordinates = [0,0,0,0]

    i = 0

    while True:

        coordinates[i%4]=pyautogui.position()

        #每过一秒钟把坐标存在坐标数组中

        # if i == 4:

        #     i=0

        # 如果四秒钟储存的坐标都相同,那么判断该店坐标为开始点坐标或者结束点坐标

        # if coordinates[0]==coordinnates[1]==coordinnates[2]==coordinnates[3]

        if coordinates.count(coordinates[i%4])==4 and coordinates[i%4]!=0: #坐标数量计算为四,坐标不为(0,0,0,0)

            return coordinates[i%4]

        time.sleep(1)

        i = i+1

# 捕捉区域图像 and ocr识别

def capture_and_ocr(start,end):     #传入开始点坐标和结束点坐标

    ocr = CnOcr()   #实例化ocr

    screenshot_filename = "tem.png"

    r = [min(start[0],end[0]),min(start[1],end[1]),abs(start[0]-end[0]),abs(start[1]-end[1])]    #region的英文单词表示区域

    #确定开始区域,abs()确保截图的长宽高为正数

    pyautogui.screenshot(imageFilename=screenshot_filename,region = r)    #将截取照片命名

    out = ocr.ocr("tem.png")    #传图片

    for i in out:

        print(i["text"])    #只显示文本内容

if __name__ == "__main__":

    print("请将鼠标移动到开始坐标......")

    start_coord = get_coordinates()

    print("开始坐标: ",start_coord)

    print("请将鼠标移动到结束坐标......")

    end_coord = get_coordinates()

    print("请将鼠标移动到结束坐标: ",end_coord)

    capture_and_ocr(start_coord,end_coord)

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值