php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 736|回复: 0

Python实现文字提取器源码

[复制链接]

2572

主题

2579

帖子

9100

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6406
贡献
0
注册时间
2021-4-14
最后登录
2024-3-28
在线时间
653 小时
QQ
发表于 2022-10-23 08:07:01 | 显示全部楼层 |阅读模式
[mw_shl_code=python,true]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([f1,f2])
    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()[/mw_shl_code]





上一篇:Python实现年报爬取源码
下一篇:栅格转矢量
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-3-29 05:24 , Processed in 0.175212 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表