-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
141 lines (125 loc) · 4.91 KB
/
app.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
import io
import os
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import Qt, QBuffer, QRect, QSize, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog, QLabel, QPushButton, QMessageBox
from PyQt5.QtGui import QPainter, QBrush, QPen, QColor, QCursor, QPalette, QIcon
from PIL import Image
import pytesseract
from pynput.mouse import Button, Controller
import time
import pyperclip
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.start, self.end = QPoint(), QPoint()
self.fullWindow()
def fullWindow(self):
self.setWindowIcon(QIcon('icon.png'))
self.setWindowTitle('Capture')
flags = Qt.WindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Dialog)
self.setWindowFlags(flags)
self.showMaximized()
self.setStyleSheet("background-color: rgba(255,255,255,0.0); border: 3px solid rgb(16, 229, 125);")
self.show()
try:
pytesseract.get_tesseract_version()
except:
self.showTesseractError()
def mousePressEvent(self, session):
mouse = Controller()
self.cropStart = mouse.position
self.start = self.end = session.pos()
self.update()
self.screen = QApplication.screenAt(QCursor.pos()).grabWindow(0)
self.screenShot = self.screen.copy(QRect(QPoint(0, 0), QSize(-1, -1)))
return super().mousePressEvent(session)
def mouseReleaseEvent(self, session):
if self.start == self.end:
return super().mouseReleaseEvent(session)
mouse = Controller()
self.cropEnd = mouse.position
self.end = session.pos()
self.update()
self.openPopup()
return super().mouseReleaseEvent(session)
def mouseMoveEvent(self, session):
self.end = session.pos()
self.update()
return super().mousePressEvent(session)
def paintEvent(self, e):
painter = QPainter(self)
painter.setPen(QPen(QColor.fromRgbF(0,0,0,1.0), 1, Qt.SolidLine))
painter.setBrush(QBrush(QColor.fromRgbF(255,255,255,0.2), Qt.SolidPattern))
painter.drawRect(QRect(self.start, self.end))
def openPopup(self):
msg = QMessageBox()
msg.setWindowTitle('Are you sure?')
msg.setText('Do you want to confirm selection?')
msg.setIcon(QMessageBox.Question)
msg.setStandardButtons(QMessageBox.Yes | QMessageBox.Retry)
msg.setDefaultButton(QMessageBox.Yes)
self.start, self.end = QPoint(), QPoint()
self.update()
self.resizedImage = self.screenShot.scaled(screenWidth, screenHeight)
self.resizedImage.save('capture.png')
self.resizedImage = Image.open('./capture.png')
msg.buttonClicked.connect(self.retry)
msg.exec()
def retry(self, i):
if(i.text() == 'Retry'):
os.remove('capture.png')
pass
else:
selectedArea = self.resizedImage.crop((self.cropStart[0], self.cropStart[1], self.cropEnd[0], self.cropEnd[1]))
selectedArea.save('selected.png')
os.remove('capture.png')
self.convertToText(selectedArea)
def convertToText(self, screenShot):
result = pytesseract.image_to_string(screenShot, timeout=10)
pyperclip.copy(result)
os.remove('selected.png')
if(result == ''):
self.displayError()
else:
print(f'Result coppied to clipboard')
self.displayExit()
def displayExit(self):
msg = QMessageBox()
msg.setWindowTitle('Coppied')
msg.setText('The obtained result has been coppied to the clpiboard')
msg.setIcon(QMessageBox.NoIcon)
msg.setStandardButtons(QMessageBox.Retry | QMessageBox.Close)
msg.setDefaultButton(QMessageBox.Close)
msg.buttonClicked.connect(self.exit)
msg.exec()
def displayError(self):
msg = QMessageBox()
msg.setWindowTitle('Error')
msg.setText('No text was found.')
msg.setIcon(QMessageBox.Warning)
msg.setStandardButtons(QMessageBox.Retry | QMessageBox.Close)
msg.setDefaultButton(QMessageBox.Retry)
msg.buttonClicked.connect(self.exit)
msg.exec()
def showTesseractError(self):
msg = QMessageBox()
msg.setWindowTitle('Error')
msg.setText('Pytesseract is either not installed or cannot be found')
msg.setIcon(QMessageBox.Critical)
msg.setStandardButtons(QMessageBox.Close)
msg.setDefaultButton(QMessageBox.Close)
msg.buttonClicked.connect(self.exit)
msg.exec()
def exit(self, i):
if(i.text() == 'Retry'):
pass
else:
QApplication.quit()
if __name__ == "__main__":
app = QApplication(sys.argv)
screenWidth = app.primaryScreen().size().width()
screenHeight = app.primaryScreen().size().height()
window = Window()
sys.exit(app.exec())