首页 > 编程语言 >python 图片转文字小工具

python 图片转文字小工具

时间:2024-02-22 14:55:50浏览次数:31  
标签:文字 python text top words 图片 side pack

应群友要求,要做一个图片转文字的格式,因为有些人的简历中只有一张图片要提取他里面的文字就不好办了。
于是就有了下面这个小工具:
功能:选择要识别的图片后,识别出来后存到.txt文本中。
实现原理,基于百度“文字识别”通用版的api接口调用。
有一点需要说明的是可能无法识别百分百准确的,格式排版还是要人为去处理一下。

代码如下:
from aip import AipOcr
from PIL import Image as PImage
from PIL import ImageTk
from tkinter import *
from tkinter import filedialog

将图片内容翻译为文字,显示在文本框内

def trans():
# """ 你的 APPID AK SK """
APP_ID = ''
API_KEY = '
'
SECRET_KEY = '
*************'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

contents.delete('1.0', END)
transTxt = client.basicGeneral(open(filePath.get(), 'rb').read())
# 对transTxt进行处理  去空格,换行符去重
transTxt=transTxt['words_result']
# transTxt=[{'words': '每个人总在仰望和'}, {'words': '羡慕着别人的幸福'}, {'words': '一回头'}, {'words': '却发现自己正被别'}, {'words': '人仰望和羡慕着'}]
words_content=''
for words in transTxt:
    values =words['words']
    words_content +=values+'\n'
print(words_content)
contents.insert(INSERT,words_content)
# 将文字保存到TXT文件
with open(filePath.get()+'_to_word.txt', 'w') as f:
    f.write(words_content)

打开图片文件,显示路径,并将图片展现

def openfile():
filename.delete('1.0', END)
filePath.set(filedialog.askopenfilename())
filename.insert(1.0, filePath.get())
org_img = PImage.open(filePath.get())
# 调整图片显示大小 600*800
w, h = org_img.size
if w > 600:
h = int(h * 600 / w)
w = 600
if h > 800:
w = int(w * 800 / h)
h = 800
img = ImageTk.PhotoImage(org_img.resize((w, h)))
showPic.config(image=img)
showPic.image = img # 保持一个引用才能显示图片,tkinter的BUG

设置主窗口

top = Tk()
top.title("图片转文字 引擎:百度云API文字识别 Made by: Running")

top.iconbitmap("./pic/y1.ico")

top.geometry("800x600")

filePath = StringVar()

第一个窗体

frame1 = Frame(top, relief=RAISED, borderwidth=2)
frame1.pack(side=TOP, fill=BOTH, ipady=5, expand=0)
Label(frame1, height=1, text="图片路径:").pack(side=LEFT)
filename = Text(frame1, height=2)
filename.pack(side=LEFT, padx=1, pady=0, expand=True, fill=X)
Button(frame1, text="打开文件", image='', command=openfile).pack(side=LEFT, padx=5, pady=0)
Button(frame1, text="识别图片", image='', command=trans).pack(side=LEFT, padx=5, pady=0)

第二个窗体

frame2 = Frame(top, relief=RAISED, borderwidth=2)
frame2.pack(side=LEFT, fill=BOTH, expand=1)
Label(frame2, text='待识别的图片:', borderwidth=5).pack(side=TOP, padx=20, pady=5)
showPic = Label(frame2, text='图片显示区')
showPic.pack(side=BOTTOM, expand=1, fill=BOTH)

第三个窗体

frame3 = Frame(top)
frame3.pack(side=RIGHT, fill=BOTH, expand=1)

contents = ScrolledText(frame3)

Label(frame3, text='图片识别结果:', borderwidth=5).pack(side=TOP, padx=20, pady=10)
contents = Text(frame3, font=('Arial', 15))
contents.pack(side=TOP, expand=1, fill=BOTH)
Label(frame3, text='Copyright 2024 baidu.com ALL Rights Reserved', borderwidth=5).pack(side=BOTTOM, padx=20, pady=10)

top.mainloop()

标签:文字,python,text,top,words,图片,side,pack
From: https://www.cnblogs.com/fishrunning/p/18027337

相关文章

  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的玉米病虫害检测系统(Python+PySide6界面+训练代码)
    摘要:本文介绍了一种基于深度学习的玉米病虫害检测系统系统的代码,采用最先进的YOLOv8算法并对比YOLOv7、YOLOv6、YOLOv5等算法的结果·,能够准确识别图像、视频、实时视频流以及批量文件中的玉米病虫害。文章详细解释了YOLOv8算法的原理,并提供了相应的Python实现代码、训练数据集,以......
  • Python:Lambda
    Lambda>>>(lambda:3)()#Usingalambdaexpressionasanoperatorinacallexp.经过查阅资料,理解了这个表达式要做的事情:(lambda:3)()是一个Lambda表达式的调用,它表示创建了一个匿名函数(Lambda函数),该函数不接受任何参数,而在执行时返回值为3。(lambda:3):这......
  • python中常见的异常 error
    python中常见的异常在python2中可以通过一个模块来查看所有的内置异常,而在python3中就无法查看。>>>importexceptions>>>dir(exceptions)['ArithmeticError','AssertionError','AttributeError','BaseException','BufferError',&#......
  • Python Web 开源框架排行榜
    截止到2024年2月22日,对GithubStar>900的PythonWeb开源框架,按照Star数量,从高到低排序,具体排名如下:1.DjangoDjangoisahigh-levelPythonwebframeworkthatencouragesrapiddevelopmentandclean,pragmaticdesign.About: TheWebframeworkforperfec......
  • python实现zip分卷压缩与解压
    1. python实现zip分卷压缩WinHex开始16进制一个一个文件对比WinRar创建的分卷压缩和单个zip文件的差异。如果想把单个大文件 test.zip ->分卷文件 test.z01、test.z02、test.zip首先,在创建的第一个分卷文件 test.z01的前面加上 \x50\x4b\x07\x08 这个是分卷压缩......
  • python在B站爬糖豆广场舞
    先附上代码:importrequests,refromlxmlimportetree#这是单页面下载,翻页的目前还不会url='https://search.bilibili.com/all?vt=96737335&keyword=%E7%B3%96%E8%B1%86%E5%B9%BF%E5%9C%BA%E8%88%9E'headers={'user-agent':'Mozilla/5.0(Window......
  • 1.3 使用pip管理Python扩展库
    常用pip命令使用方法pip命令示例说明pipfreeze[>packages.txt]列出已安装模块及其版本号,可使用重定向符>把扩展库信息保存到文件packages.txt中pipinstallSomePackage[==version]在线安装SomePackage模块,可以使用方括号内的形式指定扩展库版本pipinstallSo......
  • 1.2 Python安装与简单使用
    Python3.6.8安装Python官网:https://www.python.org/Python3.6.8官网:https://www.python.org/downloads/release/python-368/按照提示安装即可,安装完成后,按win+R打开命令行,输入python-V,出现版本号,说明安装成功在开始菜单中选择IDLE(PythonGUI)即可启动Python解释器......
  • 1.1 如何选择Python版本
    简介Python是一门解释型高级语言,支持伪编译可以把Python源程序转换为字节码来优化程序和提高运行速度,支持使用py2exe、py2app、cx_Freeze或pyinstaller工具将Python程序打包为不同平台上的可执行程序,可以在没有安装Python解释器和相关依赖包的系统中运行Python支持多版本并存......
  • Python练习案例_Pico Fermi Bagels猜数字游戏
    案例介绍--《Python编程快速上手2》在PicoFermiBagels这个逻辑推理游戏中,你要根据线索猜出一个三位数。游戏会根据你的猜测给出以下提示之一:如果你猜对一位数字但数字位置不对,则会提示“Pico”;如果你同时猜对了一位数字及其位置,则会提示“Fermi”;如果你猜测的数字及其位置......