python3打造图片文字识别软件

用python3对图片里的文字进行识别,本文使用的是tesseract-OCR识别引擎,相关的资料和文字放在本文的结尾,有需要的读者可以自行下载,软件效果图如下:
在这里插入图片描述
首先导入相应的模块:

from PIL import Image
import pytesseract

from tkinter import *
from tkinter import scrolledtext
import zipfile
import tkinter.filedialog
from PIL import Image
import threading

接着编写相应的函数:

# 选取文件的操作
def get_xuan():
    global data
    # 使用文件对话框选择文件
    # filedialog.askopenfilenames可以返回多个文件名
    data = tkinter.filedialog.askopenfilenames(title = "选择文件")
    # print(type(data),data)
    tmp = []
    for i in data:
        tmp.append(i)
    # print(tmp , type(tmp))
    # exit()
    qian = "选择的文件有:" + "\n"
    # 返回通过指定字符连接序列中元素后生成的新字符串。
    file_xian =  qian + '\n'.join(tmp)
    # print(file_xian , type(file_xian))
    # 图片文件名
    # 下载的文件名
    global filename
    filename = file_xian.rpartition('/')[-1]
    # print(filename , type(filename))

    # 待处理图片存储路径
    im = Image.open(filename)
    # Resize图片大小,入口参数为一个tuple,新的图片大小
    imBackground = im.resize((710, 490))
    # 处理后的图片的存储路径,以及存储格式
    name = 'z_1.gif'
    imBackground.save(name, 'gif')

    global pic_1
    pic_1 = PhotoImage(file='z_1.gif')
    lable14 = Label(window, image=pic_1)
    lable14.place(x=280, y=0)


def get_show():
    text=pytesseract.image_to_string(Image.open(filename),lang='chi_sim')
    result = text + '\n' + '\n'
    scr.insert(END , result)



def thread_it(func):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    # t.join()

最后进行GUI界面的布局:

window = Tk()
# 设置标题
window.title('图片文字识别')
# 窗口的位置和大小
window.geometry('1000x700+300+65')
# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)
window.config(bg = "black")

pic = PhotoImage(file='logo.gif')
lable14 = Label(window, image=pic)
lable14.place(x=30, y=0)


button0=Button(window,text="选择图片", font=('微软雅黑', 25),bg = '#126EC5' , command = lambda: thread_it(get_xuan))
button0.place(x=30,y = 200,width = 150)

button1 = Button(window,text = '开始识别', font=('微软雅黑', 25),bg = '#126EC5' , command = lambda: thread_it(get_show))
button1.place(x = 30,y = 320,width = 150)

lable1 = Label(window, text=' 打印结果:', font=('微软雅黑', 30), fg='black' , bg = '#126EC5')
lable1.place(x=20, y=450)

lable2 = Label(window, text=' 图片展示区', font=('微软雅黑', 60), fg='black' , bg = '#126EC5')
lable2.place(x=400, y=200)

for i in range(10):
    lable2 = Label(window, text='|', font=('微软雅黑', 30), fg='black' , bg = 'Snow')
    lable2.place(x=260, y= i * 50)

for j in range(14):
    lable3 = Label(window, text='—', font=('微软雅黑', 30), fg='black' , bg = 'Snow')
    lable3.place(x=280 + j * 50, y=470)

scr = scrolledtext.ScrolledText(window, font=('微软雅黑', 15), width=80, height=6)
scr.place(x=10, y=520)

window.mainloop()

本文感谢博文的技术支持:https://blog.csdn.net/qiushi_1990/article/details/78041375
相关的资料和环境配置:https://blog.csdn.net/showgea/article/details/82656515
代码到这里就完成了,喜欢博文的读者可以关注博主哦,我是活动的笑脸。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值