基于PyQt5如何制作图转文功能,方法是什么
Admin 2022-08-11 群英技术资讯 278 次浏览
本文就是基于PyQt5完成的图转文功能,先看界面:
from PyQt5 import QtCore, QtGui, QtWidgets import base64 import os from return_png import img as retu # 引入img变量,赋别名为pdf_cf class Ui_PicZ(object): def setupUi(self, PicZ): tmp = open('return.png', 'wb') # 创建临时的文件 tmp.write(base64.b64decode(retu)) # 把这个one图片解码出来,写入文件中去。 tmp.close() PicZ.setObjectName("PicZ") PicZ.resize(1400, 814) PicZ.setMinimumSize(QtCore.QSize(1400, 814)) PicZ.setMaximumSize(QtCore.QSize(1400, 814)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) PicZ.setPalette(palette) self.Reback = QtWidgets.QToolButton(PicZ) self.Reback.setGeometry(QtCore.QRect(10, 10, 51, 41)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush) self.Reback.setPalette(palette) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("return.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.Reback.setIcon(icon) self.Reback.setIconSize(QtCore.QSize(100, 100)) self.Reback.setAutoRaise(True) self.Reback.setObjectName("Reback") self.label = QtWidgets.QLabel(PicZ) self.label.setGeometry(QtCore.QRect(88, 8, 141, 41)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(16) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.frame = QtWidgets.QFrame(PicZ) self.frame.setGeometry(QtCore.QRect(20, 60, 1351, 161)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame.setPalette(palette) self.frame.setAutoFillBackground(True) self.frame.setFrameShape(QtWidgets.QFrame.Box) self.frame.setFrameShadow(QtWidgets.QFrame.Plain) self.frame.setLineWidth(1) self.frame.setMidLineWidth(0) self.frame.setObjectName("frame") self.scwd = QtWidgets.QPushButton(self.frame) self.scwd.setGeometry(QtCore.QRect(570, 30, 191, 61)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.scwd.setPalette(palette) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(16) font.setBold(True) font.setWeight(75) self.scwd.setFont(font) self.scwd.setAutoFillBackground(True) self.scwd.setAutoDefault(False) self.scwd.setDefault(False) self.scwd.setFlat(True) self.scwd.setObjectName("scwd") self.label_2 = QtWidgets.QLabel(self.frame) self.label_2.setGeometry(QtCore.QRect(490, 110, 371, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.frame_2 = QtWidgets.QFrame(PicZ) self.frame_2.setGeometry(QtCore.QRect(1050, 280, 321, 301)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame_2.setPalette(palette) self.frame_2.setAutoFillBackground(True) self.frame_2.setFrameShape(QtWidgets.QFrame.NoFrame) self.frame_2.setFrameShadow(QtWidgets.QFrame.Plain) self.frame_2.setLineWidth(1) self.frame_2.setMidLineWidth(0) self.frame_2.setObjectName("frame_2") self.label_3 = QtWidgets.QLabel(self.frame_2) self.label_3.setGeometry(QtCore.QRect(20, 20, 191, 31)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(14) font.setBold(False) font.setWeight(50) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.xzwd = QtWidgets.QPushButton(self.frame_2) self.xzwd.setGeometry(QtCore.QRect(30, 100, 271, 61)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.xzwd.setPalette(palette) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.xzwd.setFont(font) self.xzwd.setAutoFillBackground(True) self.xzwd.setAutoDefault(False) self.xzwd.setDefault(False) self.xzwd.setFlat(True) self.xzwd.setObjectName("xzwd") self.label_4 = QtWidgets.QLabel(PicZ) self.label_4.setGeometry(QtCore.QRect(20, 280, 500, 500)) self.label_4.setFrameShape(QtWidgets.QFrame.Box) self.label_4.setText("") self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(PicZ) self.label_5.setGeometry(QtCore.QRect(530, 280, 500, 500)) self.label_5.setFrameShape(QtWidgets.QFrame.Box) self.label_5.setText("") self.label_5.setObjectName("label_5") self.label_7 = QtWidgets.QLabel(PicZ) self.label_7.setGeometry(QtCore.QRect(210, 250, 71, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_7.setFont(font) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(PicZ) self.label_8.setGeometry(QtCore.QRect(730, 250, 71, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_8.setFont(font) self.label_8.setObjectName("label_8") self.retranslateUi(PicZ) QtCore.QMetaObject.connectSlotsByName(PicZ) os.remove("return.png") def retranslateUi(self, PicZ): _translate = QtCore.QCoreApplication.translate PicZ.setWindowTitle(_translate("PicZ", "图片转文字")) self.Reback.setText(_translate("PicZ", "...")) self.label.setText(_translate("PicZ", "图转文")) self.scwd.setText(_translate("PicZ", "+ 上传图片")) self.label_2.setText(_translate("PicZ", "<html><head/><body><p>上传图片至此 支持格式:jpg, png,bmp,gif,tiff等</p></body></html>")) self.label_3.setText(_translate("PicZ", "操作")) self.xzwd.setText(_translate("PicZ", "下载Word文档")) self.label_7.setText(_translate("PicZ", "图片显示")) self.label_8.setText(_translate("PicZ", "识别结果"))
class textwindow(QtWidgets.QWidget,Ui_PicZ): def __init__(self): super(textwindow,self).__init__() self.setupUi(self) def re_back(self): self.close() def sctp(self): fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"自定义(*.jpg;*.png;*.jpeg;*);;All Files(*)") image = QPixmap(fileName) f = str(fileName) self.p, self.s = f.split(".") resImg1 = image.scaled(QSize(500, 500)) self.label_4.setPixmap(resImg1) self.label_4.setScaledContents(True) self.text = pytesseract.image_to_string(f, lang="chi_sim+eng", config='--psm 6') # 打印识别后的文本d self.label_5.setText(self.text) def xzwj(self): document=Document() document.add_paragraph(self.text) document.save("{}_图转文.docx".format(self.p)) root = tkinter.Tk() root.withdraw() tkinter.messagebox.showinfo("提示", "成功下载到源文件所在文件夹")
说明如下:
(1) def sctp(self): 其中的sctp为按钮的objectName属性名字;def xzwj(self):其中的xzwj为按钮的objectName属性名字;
(2) 可通过点击按钮实现本地文件上传;
fileName, fileType = QtWidgets. QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "自定义(*.jpg;*.png;*.jpeg;*);;All Files(*)")
(3) 利用label组件显示图片,同时自适应图片;
(4) 关键点为借助pytesseract模块实现离线模式图转文,在安装过程中务必安装语言包,否则功能不会实现,具体如何安装,大家可自行搜索;
(5) 最关键可实现图转文结果页会在label组件中显示。
if __name__ =="__main__": import sys app = QtWidgets.QApplication(sys.argv) win = mywindow() pt = textwindow() win.show() win.Pic_SB.clicked.connect(win.pic_main) win.Pic_SB.clicked.connect(pt.show) pt.Reback.clicked.connect(pt.re_back) pt.Reback.clicked.connect(win.show) pt.scwd.clicked.connect(pt.sctp) pt.xzwd.clicked.connect(pt.xzwj) sys.exit(app.exec_())
本文介绍的图转文功能还有一些待完善地方就是pytesseract该模块,不会很清晰的显示所有文字,不是对所有照片都好用,有时还会死机。当然我们可以在后续阶段逐渐完善。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了PyHacker实现网站后台扫描器编写指南,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要为大家详细介绍了Python实现双人五子棋对局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
要想在Flask中处理好异常,有一套自己的异常处理机制,首先,我们必须先知道Flask自己是如何处理异常的。去flask的源码里找一找会发现,在flask源码的app.py文件下,有很多会抛出异常的方法,其中拿一个举例:
在实际工作中,经常涉及到数据的传递。这篇文章主要为大家介绍了Python的一些基础学习:深拷贝与浅拷贝问题、递归函数的练习,需要的朋友可以参考一下
这篇文章主要介绍了使用pandas两列转换成字典的健和值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008