admin 发表于 2022-10-23 08:07:01

Python实现文字提取器源码

import tkinter as tk
from tkinter import filedialog
from PIL import Image,ImageTk
import requests
import base64
def resize(w,h,newW,newH,pilImage):
    f1 = 1.0 * newW / w
    f2 = 1.0 * newH / h
    factor = min()
    width = int(w * factor)
    height = int(h * factor)
    return pilImage.resize((width,height),Image.ANTIALIAS)

apiKey = 'Vo9OFzDiLi2SNv7rd5L8FDh2'
secretKey = 'caaVMYOdjcgW70c4M8QTNS7cvh0KQDLW'
def getToken():
    getTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+apiKey+'&client_secret='+secretKey
    response = requests.get(getTokenUrl)
    data = response.json()
    token = data.get('access_token')
    return token

def getResult(imagePath):
    url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/webimage'
    #设置图片编码
    with open(imagePath,'rb') as f:
      image = f.read()
    b64Image = base64.b64encode(image)
    #设置请求参数
    params = {'access_token': getToken()}
    data = {'image': b64Image}
    #发送网络请求
    response = requests.post(url,params=params,data=data)
    content = response.json()
    result = content['words_result']
    print(result)
    #将内容写入文件
    for i in result:
      with open("file/text.txt",'a',encoding='utf-8') as f:
            f.write(i['words']+'\n')
    #界面跳转
    window.destroy()
    newWindow = tk.Tk()
    newWindow.resizable(0,0)
    newWindow.title('文本编辑框')
    bg = ImageTk.PhotoImage(file="images/bg2.jpg")
    bgLabel = tk.Label(newWindow,width=1000,height=600,image=bg)
    bgLabel.pack()
    #请在下方书写你的代码
    #显示识别文本
    with open("file/text.txt",'r',encoding='utf-8') as f:
      words = f.read()
    text = tk.Text(newWindow,width=39,height=13,font=("font/simhei.ttf",18))
    text.place(x=92,y=144)
    text.insert(tk.INSERT,words)
    #添加保存按钮
    saveImg = ImageTk.PhotoImage(file="images/save.png")
    saveBtn = tk.Button(newWindow,image=saveImg,width=163,height=59,bd=0,command=lambda:save(text))
    saveBtn.place(x=700,y=300)
    newWindow.mainloop()
#请在下方书写你的代码
#保存文本
def save(text):
    content = text.get('1.0',tk.END)
    with open("file/text.txt",'w',encoding='utf-8') as f:
      f.write(content)

def chooseImage():
    global imagePath
    imagePath = filedialog.askopenfilename(initialdir="./img",title='Choose an image.')
    imageFile = Image.open(imagePath)
    w,h = imageFile.size
    #绘制选择的图片
    scaleImg = resize(w,h,450,290,imageFile)
    chooseImg = ImageTk.PhotoImage(scaleImg)
    chooseLab = tk.Label(window,image=chooseImg,width=455,height=285)
    chooseLab.place(x=270,y=130)
    window.mainloop()

window = tk.Tk()
window.resizable(0,0)
window.title('文字提取器')
bg = ImageTk.PhotoImage(file="images/bg.jpg")
bgLabel = tk.Label(window,width=1000,height=600,image=bg)
bgLabel.pack()
#选择图片按钮
chooseImg = ImageTk.PhotoImage(file="images/choose.png")
choose = tk.Button(window,image=chooseImg,width=242,height=71,bd=0,command=chooseImage)
choose.place(x=200,y=500)
#识别文字按钮
startImg = ImageTk.PhotoImage(file="images/start.png")
start = tk.Button(window,image=startImg,width=242,height=72,bd=0,command= lambda:getResult(imagePath))
start.place(x=560,y=500)
window.mainloop()
页: [1]
查看完整版本: Python实现文字提取器源码