暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Python实现的图片识别工具

原创 zayki 2021-08-16
658
# -*- coding: utf-8 -*- import os import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox from PIL import Image import pytesseract button_style = ''' QPushButton {text-align : center; background-color : white; font: bold; border-color: gray; border-width: 1px; border-radius: 3px; padding: 2px; height : 14px; border-style: outset; font : 12px;} QPushButton:hover {text-align : center; background-color : cyan; font: bold; border-color: gray; border-width: 1px; border-radius: 5px; padding: 1px; height : 14px; border-style: outset; font : 12px;} QPushButton:pressed {text-align : center; background-color : cyan; font: bold; border-color: gray; border-width: 1px; border-radius: 5px; padding: 1px; height : 14px; border-style: outset; font : 12px;} ''' class Ui_img2str(object): def __init__(self): self.languages = { "简体中文":'chi_sim', "繁体中文":'chi_tra', "English":'eng' } def setupUi(self, img2str): img2str.setObjectName("img2str") img2str.setFixedSize(629, 348) img2str.setWindowIcon(QIcon('ocr.ico')) self.textBrowser = QtWidgets.QTextBrowser(img2str) self.textBrowser.setGeometry(QtCore.QRect(10, 80, 611, 261)) self.textBrowser.setObjectName("textBrowser") self.label = QtWidgets.QLabel(img2str) self.label.setGeometry(QtCore.QRect(100, 0, 471, 41)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(24) self.label.setFont(font) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(img2str) self.label_2.setGeometry(QtCore.QRect(360, 20, 251, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(10) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.widget = QtWidgets.QWidget(img2str) self.widget.setGeometry(QtCore.QRect(10, 50, 611, 25)) self.widget.setObjectName("widget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setObjectName("horizontalLayout") self.toolButton = QtWidgets.QPushButton(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(10) sizePolicy.setVerticalStretch(10) sizePolicy.setHeightForWidth(self.toolButton.sizePolicy().hasHeightForWidth()) self.toolButton.setSizePolicy(sizePolicy) self.toolButton.setMaximumSize(QtCore.QSize(100, 50)) self.toolButton.setObjectName("toolButton") self.toolButton.setStyleSheet(button_style) self.horizontalLayout.addWidget(self.toolButton) self.lineEdit = QtWidgets.QTextEdit(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth()) self.lineEdit.setSizePolicy(sizePolicy) self.lineEdit.setMaximumSize(QtCore.QSize(16777213, 50)) self.lineEdit.setObjectName("lineEdit") self.horizontalLayout.addWidget(self.lineEdit) self.comboBox = QtWidgets.QComboBox(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.comboBox.sizePolicy().hasHeightForWidth()) self.comboBox.setSizePolicy(sizePolicy) self.comboBox.setMaximumSize(QtCore.QSize(100, 50)) self.comboBox.setObjectName("comboBox") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.addItem("") self.comboBox.setStyleSheet("QComboBox {" "combobox-popup: 1;\n" # 滚动条设置必需 "border: 1px; " "padding-left:0px; " # 字体距离左边的距离 "width:80px; " "height:20px; " "font-size:14px; " "font-family:微软雅黑; " "font-weight:400; " "color:rgba(93,169,255,1);\n" "line-height:20px; }\n" "QComboBox:drop-down {" # 选择箭头样式 "width:10px; " "height:80px; " "border: 1px; " "subcontrol-position: right center; " # 位置 "subcontrol-origin: padding;}\n" # 对齐方式 "QComboBox:down-arrow {" # 选择箭头,继承drop-down "border: 1px; " "background: transparent; " "QComboBox QAbstractItemView {" # 下拉选项样式 "color:black; " "background: transparent; " "selection-color:rgba(93,169,255,1);" "selection-background-color: rgba(255,255,255,1);" "}\n" "QComboBox QAbstractScrollArea QScrollBar:vertical {" # 滚动条样式 "width: 6px;\n" "height: 100px;" "background-color: transparent; }\n" "QComboBox QAbstractScrollArea QScrollBar::handle:vertical {\n" # 滚动条样式 "border-radius: 3px; " "background: rgba(0,0,0,0);}\n" "QComboBox QAbstractScrollArea QScrollBar::handle:vertical:hover {\n" # 划过滚动条,变化 "background: rgb(90, 91, 93);}\n" "QComboBox QScrollBar::add-line::vertical{" # 滚动条上箭头 "border:none;}" "QComboBox QScrollBar::sub-line::vertical{" # 滚动条下箭头 "border:none;}" "") self.horizontalLayout.addWidget(self.comboBox) self.pushButton = QtWidgets.QPushButton(self.widget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) self.pushButton.setSizePolicy(sizePolicy) self.pushButton.setMaximumSize(QtCore.QSize(100, 50)) self.pushButton.setObjectName("pushButton") self.pushButton.setStyleSheet(button_style) self.horizontalLayout.addWidget(self.pushButton) self.retranslateUi(img2str) QtCore.QMetaObject.connectSlotsByName(img2str) self.pushButton.clicked.connect(self.img_to_str) self.toolButton.clicked.connect(self.choose_img_path) self.lineEdit.textChanged.connect(self.drag_img_path) def choose_img_path(self): open_file_path = QFileDialog.getOpenFileName(self.toolButton,"Open file", os.getenv('USERPROFILE') + "\\Desktop", "BMP files(*.bmp);;PNG files(*.png);;JPEG files(*.jpeg);;JPG files(*.jpg)") self.lineEdit.setText(open_file_path[0]) def drag_img_path(self): if 0 == self.lineEdit.toPlainText().find('file:///'): self.lineEdit.setText(self.lineEdit.toPlainText().replace('file:///', '')) def messageBox(self, title, text): messagebox = QMessageBox() messagebox.setWindowIcon(QIcon('ocr.ico')) messagebox.setWindowTitle(title) messagebox.setStyleSheet(button_style) messagebox.setText(text) messagebox.addButton(QtWidgets.QPushButton('确定'), QMessageBox.YesRole) messagebox.exec_() def img_to_str(self): self.textBrowser.clear() image_path = self.lineEdit.toPlainText() choose_ind = self.comboBox.currentText() lang = self.languages[choose_ind] try: result = pytesseract.image_to_string(Image.open(image_path), lang) self.textBrowser.setText(result) clipboard = QApplication.clipboard() clipboard.setText(self.textBrowser.toPlainText()) except: self.messageBox('提示',"加载或者处理图片异常!") def retranslateUi(self, img2str): _translate = QtCore.QCoreApplication.translate img2str.setWindowTitle(_translate("img2str", "OCR工具")) self.label.setText(_translate("img2str", "图片文字识别工具 ")) self.label_2.setText(_translate("img2str", "仅支持简体中文、繁体中文以及英文和数字")) self.toolButton.setText(_translate("img2str", "选择图片")) self.comboBox.setItemText(0, _translate("img2str", "English")) self.comboBox.setItemText(1, _translate("img2str", "简体中文")) self.comboBox.setItemText(2, _translate("img2str", "繁体中文")) self.pushButton.setText(_translate("img2str", "转换并复制")) if __name__ == "__main__": app = QApplication(sys.argv) form = QWidget() w = Ui_img2str() w.setupUi(form) form.show() sys.exit(app.exec_())

需要安装Tesseract-OCR,并且将tesseract.exe配置到环境变量path路径。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论