Snipaste 是我最喜欢的截图软件可惜没有 OCR 功能
依赖:BaiduAPI + Python + baidu-aip + win10toast + pillow + clipboard + keyboard + chardet
感谢 小小 大佬,本文脚本代码基于此文章修改
百度OCR接口申请不再赘述,请参考自行有关文章
from win10toast import ToastNotifier # 识别成功向 windows 发送桌面通知from PIL import ImageGrab # 读取剪切板图片并保存from aip import AipOcr # 百度 APIimport clipboard # 识别内容写入剪切板import keyboard # 监听键盘import timedef screenshot(): # 输入键盘的触发事件 # F1 截图 keyboard.wait(hotkey="f1") # ctrl+c 保存剪切板 keyboard.wait(hotkey="ctrl+c") time.sleep(0.5) # 2.保存图片 # 把图片从剪切板保存到当前路径 image = ImageGrab.grabclipboard() image.save(r'C:\Users\Methanol\Documents\Code\Python Script\img\screen_temp.png')def baidu_api(client): with open(r'C:\Users\Methanol\Documents\Code\Python Script\img\screen_temp.png', 'rb') as f: image = f.read() # 调用百度API通用文字识别(高精度版),提取图片中的内容 text = client.basicAccurate(image) result = text["words_result"] copyText = [] # 将识别内容写入剪切板 for i in result: copyText.append(i["words"]) clipboard.copy('\n'.join(copyText))def notice(): # windows10 通知提醒,OCR识别完成时进行通知 toaster = ToastNotifier() toaster.show_toast("OCR", "文字识别成功", icon_path=r'C:\Users\Methanol\Documents\Code\Python Script\img\OCR.ico', duration=10)if __name__ == '__main__': APP_ID = 'APP_ID' API_KEY = 'API_KEY' SECRET_KEY = 'SECRET_KEY' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) while True: screenshot() time.sleep(0.1) baidu_api(client) notice()注意修改下文 CodePath 与 PythonwPath,其中 CodePath 为脚本的绝对路径
PythonwPath ?在 CMD 中键入 where pythonw ,将获取的路径删除末尾 pythonw.exe 即为 PythonwPath
-->任务计划程序pythonw"CodePath" (不能省略双引号)PythonwPath