-
Notifications
You must be signed in to change notification settings - Fork 0
/
shottext.py
136 lines (109 loc) · 4.23 KB
/
shottext.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
import io
import os
import sys
import pyperclip
import pytesseract
from PyQt5 import QtCore, QtGui, QtWidgets
from PIL import Image
from PyQt5.QtCore import Qt
try:
from pynotifier import Notification
except ImportError:
pass
class Snipper(QtWidgets.QWidget):
def __init__(self, parent=None, flags=Qt.WindowFlags()):
super().__init__(parent=parent, flags=flags)
self.setWindowTitle("ShotText")
self.setWindowFlags(
Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Dialog
)
self.setWindowState(self.windowState() | Qt.WindowFullScreen)
self.screen = QtWidgets.QApplication.screenAt(QtGui.QCursor.pos()).grabWindow(0)
palette = QtGui.QPalette()
palette.setBrush(self.backgroundRole(), QtGui.QBrush(self.screen))
self.setPalette(palette)
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.start, self.end = QtCore.QPoint(), QtCore.QPoint()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
QtWidgets.QApplication.quit()
return super().keyPressEvent(event)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(Qt.NoPen)
painter.setBrush(QtGui.QColor(0, 0, 0, 100))
painter.drawRect(0, 0, self.width(), self.height())
if self.start == self.end:
return super().paintEvent(event)
painter.setPen(QtGui.QPen(QtGui.QColor(255, 255, 255), 3))
painter.setBrush(painter.background())
painter.drawRect(QtCore.QRect(self.start, self.end))
return super().paintEvent(event)
def mousePressEvent(self, event):
self.start = self.end = event.pos()
self.update()
return super().mousePressEvent(event)
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
return super().mousePressEvent(event)
def mouseReleaseEvent(self, event):
if self.start == self.end:
return super().mouseReleaseEvent(event)
self.hide()
QtWidgets.QApplication.processEvents()
shot = self.screen.copy(QtCore.QRect(self.start, self.end))
processImage(shot)
QtWidgets.QApplication.quit()
def processImage(img):
buffer = QtCore.QBuffer()
buffer.open(QtCore.QBuffer.ReadWrite)
img.save(buffer, "PNG")
pil_img = Image.open(io.BytesIO(buffer.data()))
buffer.close()
try:
result = pytesseract.image_to_string(
pil_img, timeout=5, lang=(sys.argv[1] if len(sys.argv) > 1 else None)
)
except RuntimeError as error:
print(f"ERROR: An error occurred when trying to process the image: {error}")
notify(f"An error occurred when trying to process the image: {error}")
return
if result:
pyperclip.copy(result)
print(f'INFO: Copied "{result}" to the clipboard')
notify(f'Copied "{result}" to the clipboard')
else:
print(f"INFO: Unable to read text from image, did not copy")
notify(f"Unable to read text from image, did not copy")
def notify(msg):
try:
Notification(title="ShotText", description=msg).send()
except (SystemError, NameError):
trayicon = QtWidgets.QSystemTrayIcon(
QtGui.QIcon(
QtGui.QPixmap.fromImage(QtGui.QImage(1, 1, QtGui.QImage.Format_Mono))
)
)
trayicon.show()
trayicon.showMessage("ShotText", msg, QtWidgets.QSystemTrayIcon.NoIcon)
trayicon.hide()
if __name__ == "__main__":
try:
pytesseract.get_tesseract_version()
except EnvironmentError:
notify(
"Tesseract is either not installed or cannot be reached.\n"
"Have you installed it and added the install directory to your system path?"
)
print(
"ERROR: Tesseract is either not installed or cannot be reached.\n"
"Have you installed it and added the install directory to your system path?"
)
sys.exit()
QtCore.QCoreApplication.setAttribute(Qt.AA_DisableHighDpiScaling)
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
snipper = Snipper(window)
snipper.show()
sys.exit(app.exec_())