-
Notifications
You must be signed in to change notification settings - Fork 0
/
image2text.py
124 lines (103 loc) · 4.29 KB
/
image2text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QTextEdit, QPushButton, QFileDialog, QVBoxLayout, QHBoxLayout, QWidget
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
from PIL import Image
import pytesseract
# MainWindow class that sets up the GUI
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set the window title and geometry
self.setWindowTitle("Image2Text")
self.setGeometry(100, 100, 600, 500)
self.setMinimumSize(300, 250)
# Create header label and buttons for opening and converting
self.label_header = QLabel("Image2Text", self)
self.label_header.setAlignment(Qt.AlignCenter)
font = self.label_header.font()
font.setPointSize(20)
self.label_header.setFont(font)
self.open_button = QPushButton("Open Image", self)
self.open_button.clicked.connect(self.open_image)
self.convert_button = QPushButton("Convert", self)
self.convert_button.clicked.connect(self.convert)
# Create image label and text box
self.label_image = QLabel(self)
self.label_image.setAlignment(Qt.AlignCenter)
self.text_box = QTextEdit(self)
# Create horizontal layout for header
layout_header = QHBoxLayout()
layout_header.addWidget(self.label_header)
# Create horizontal layout for open button
layout_open = QHBoxLayout()
layout_open.addWidget(self.open_button)
# Create vertical layout for image label
layout_image = QVBoxLayout()
layout_image.addWidget(self.label_image)
# Create horizontal layout for convert button
layout_convert = QHBoxLayout()
layout_convert.addWidget(self.convert_button)
# Create main vertical layout and add other layouts and widgets
layout_main = QVBoxLayout()
layout_main.addLayout(layout_header)
layout_main.addLayout(layout_open)
layout_main.addLayout(layout_image)
layout_main.addLayout(layout_convert)
layout_main.addWidget(self.text_box)
# Create widget and set main layout
widget = QWidget(self)
widget.setLayout(layout_main)
self.setCentralWidget(widget)
# Set the path to Tesseract
pytesseract.pytesseract.tesseract_cmd = os.path.join(
os.getcwd(), 'Tesseract-OCR/tesseract')
# Method to open image file
def open_image(self):
# Open a file dialog to select an image
self.file = QFileDialog.getOpenFileName(
self, "Open Image", "", "Images (*.png *.xpm *.jpg *.bmp *.gif);;All Files (*)")[0]
# If a file is selected
if self.file:
# Set the file path as the previous file
self.previous = self.file
# If the file path is longer than 30 characters
if len(self.file) >= 30:
# Truncate the file path and set it as the text on the open button
self.open_button.setText(
self.file[:30] + "..." + self.file[-3:])
else:
# Set the file path as the text on the open button
self.open_button.setText(self.file)
# Open the image file using PIL
self.image = Image.open(self.file)
# Create a QPixmap from the image file
pixmap = QPixmap(self.file)
# Set the pixmap on the image label
self.label_image.setPixmap(pixmap)
else:
# If a file was previously selected, set it as the current file
if self.previous != "":
self.file = self.previous
def convert(self):
# If no file has been selected
if self.file == "":
return
try:
# Use pytesseract to extract text from the image
result = pytesseract.image_to_string(self.image)
except Exception as e:
# Print the error message if there is an exception
print("Error:", e)
return
# Set the extracted text on the text box
self.text_box.setText(result)
# Create a QApplication
app = QApplication(sys.argv)
# Create the MainWindow instance
window = MainWindow()
# Show the window
window.show()
# Exit the application when the window is closed
sys.exit(app.exec_())