大家好,又见面了,我是你们的朋友全栈君。
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()
软件截图如下:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131907.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...