截图文字识别工具

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()

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值