-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (123 loc) · 5.81 KB
/
Copy pathmain.py
File metadata and controls
139 lines (123 loc) · 5.81 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import sys
import os
import time
import subprocess
import pyperclip
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QGridLayout, QFileDialog, QPlainTextEdit, QStyle
from PyQt5.QtCore import pyqtSlot, Qt
from PIL import ImageGrab
import pyautogui
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'ScreenGrab-OCR'
self.left = 100
self.top = 100
self.width = 640
self.height = 240
self.setWindowIcon(self.style().standardIcon(getattr(QStyle, 'SP_DesktopIcon')))
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.grid = QGridLayout()
self.setLayout(self.grid)
self.label = QLabel(self)
self.label.setAlignment(Qt.AlignCenter)
self.label.setText("Click the button below to capture or load a screenshot.")
self.grid.addWidget(self.label, 0, 0)
self.button = QPushButton('Capture Screenshot (Whole Screen)', self)
self.button.setIcon(self.style().standardIcon(getattr(QStyle, 'SP_DesktopIcon')))
self.button.setToolTip('Click to capture a screenshot of your entire screen. (Not recommended)')
self.button.clicked.connect(self.on_click_capture)
self.grid.addWidget(self.button, 1, 0)
self.button = QPushButton('Upload Screenshot', self)
self.button.setIcon(self.style().standardIcon(getattr(QStyle, 'SP_DialogOpenButton')))
self.button.setToolTip('Click to upload it from the local storage.')
self.button.clicked.connect(self.on_click_upload)
self.grid.addWidget(self.button, 2, 0)
self.button = QPushButton('Load Screenshot from Clipboard', self)
self.button.setIcon(self.style().standardIcon(getattr(QStyle, 'SP_FileDialogContentsView')))
self.button.setToolTip('Click to upload it from your clipboard.')
self.button.clicked.connect(self.on_click_clipboard)
self.grid.addWidget(self.button, 3, 0)
self.textbox = QPlainTextEdit(self)
self.textbox.setReadOnly(True)
self.textbox.setPlainText("Ready to capture a screenshot.\nRequires installation of Tesseract Open Source OCR Engine.\nLink: https://tesseract-ocr.github.io/tessdoc/Installation.html")
self.grid.addWidget(self.textbox, 4, 0)
self.show()
@pyqtSlot()
def on_click_capture(self):
self.label.setText("Processing...")
self.button.setEnabled(False)
self.textbox.setPlainText("")
self.update()
time.sleep(0.1)
t_start = time.perf_counter()
self.capture_screenshot()
self.perform_ocr()
self.button.setEnabled(True)
self.label.setText("The OCR result has been copied to your clipboard.\nLast run: {:.2f} seconds".format(time.perf_counter() - t_start))
self.update()
def on_click_upload(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filename, _ = QFileDialog.getOpenFileName(self,"Upload the screenshot from local storage.", "","All Files (*);;PNG Files (*.png)", options=options)
if filename:
self.label.setText("Processing...")
self.button.setEnabled(False)
self.textbox.setPlainText("")
self.update()
time.sleep(0.1)
t_start = time.perf_counter()
self.perform_ocr(filename)
self.button.setEnabled(True)
self.label.setText("The OCR result has been copied to your clipboard.\nLast run: {:.2f} seconds".format(time.perf_counter() - t_start))
self.update()
def on_click_clipboard(self):
self.label.setText("Processing...")
self.button.setEnabled(False)
self.textbox.setPlainText("")
self.update()
time.sleep(0.1)
t_start = time.perf_counter()
try:
imgrab = ImageGrab.grabclipboard()
imgrab.save('screenshot.png','PNG')
self.perform_ocr()
self.button.setEnabled(True)
self.label.setText("The OCR result has been copied to your clipboard.\nLast run: {:.2f} seconds".format(time.perf_counter() - t_start))
self.update()
except:
self.label.setText("Click the button below to capture or load a screenshot.")
self.textbox.setPlainText("No image found in the clipboard.")
self.button.setEnabled(True)
self.update()
return
def capture_screenshot(self):
width, height= pyautogui.size()
monitor_screen = (0, 0, width, height)
scrgrab = pyautogui.screenshot(region=(monitor_screen))
scrgrab.save(r'screenshot.png')
def perform_ocr(self, filename="screenshot.png"):
try:
subprocess.run(["tesseract", filename, "screenshot"])
except FileNotFoundError:
self.textbox.setPlainText("Tesseract is not installed. Please install it first.")
return
except Exception as e:
self.textbox.setPlainText("An error occurred while performing OCR.\nError: {}".format(e))
return
with open("screenshot.txt", "r") as f:
text = f.read()
pyperclip.copy(text)
self.textbox.setPlainText(text)
# Delete 'screenshot.png' and 'screenshot.txt' files if it exists in the current directory
if os.path.exists("screenshot.png"):
os.remove("screenshot.png")
if os.path.exists("screenshot.txt"):
os.remove("screenshot.txt")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())