截图文字识别工具

tkinter程序源码:初识Python,如有不足请多指教。

import tkinter as tk
import keyboard     # 安装: pip install keyboard
from PIL import ImageGrab   # pip install pillow
import time
from aip import AipOcr  # pip install baidu-api或者下载sdk后解压执行:执行python setup.py install


def center(win):
    """窗口居中函数"""
    win.update_idletasks()
    width = win.winfo_width() + 400
    height = win.winfo_height() + 260
    x = (win.winfo_screenwidth() // 2) - (width // 2)
    y = (win.winfo_screenheight() // 2) - (height // 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))


window = tk.Tk()
window.title('截图文字识别工具')
# window.geometry('600x400')
center(window)


def insert_point():
    APP_ID = '你的app_id'
    API_KEY = '你的api_key'
    SECRET_KEY = '你的secret_key'
	# 调用百度ocr的API,申请方式请自行百度。
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
    # 截取图片(截图软件)
    keyboard.wait(hotkey='alt+a')
    keyboard.wait(hotkey='enter')
    # 防止图片还未缓存就调用
    time.sleep(0.1)
    # 保存图片到电脑
    image = ImageGrab.grabclipboard()
    image.save('WzTp.jpg')
    """ 读取图片 """
    with open('WzTp.jpg', 'rb') as file:
        image = file.read()
        text = client.basicAccurate(image)
        res = text['words_result']
        for i in res:
            print(i['words'])
            # var = e.get()
            t.insert('insert', i['words']+'\n')
    t.insert('insert', '\n-----------------------------------------------------------------\n')


b1 = tk.Button(window, text='启动程序', width=20, height=2, command=insert_point)   #光标处插入
b1.pack()
t = tk.Text(window, height=25)
t.pack()
l = tk.Label(window, text='使用方法:Alt+A截图,然后Enter结束', width=30, height=1)
l.pack(expand='yes', fill='x')
window.mainloop()

软件截图如下:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章