-
Notifications
You must be signed in to change notification settings - Fork 0
/
snip.py
83 lines (67 loc) · 2.11 KB
/
snip.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
import sys
from PyQt5.QtCore import Qt, QRect, QPoint, QRectF
from PyQt5.QtGui import QPixmap, QPainter, QPainterPath, QBrush, QColor, QScreen
from PyQt5.QtWidgets import (
QWidget, QApplication, QMainWindow, QPushButton, QFileDialog
)
import copy
from pathlib import Path
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.ex = Snip()
def initUI(self):
startButton = QPushButton("Start", self)
startButton.move(50, 50)
startButton.clicked.connect(self.clickedStartButton)
self.setGeometry(300, 300, 250, 100)
self.setWindowTitle('board capture')
self.show()
def clickedStartButton(self):
self.ex.showFullScreen()
class Snip(QWidget):
endpos = QPoint(0, 0)
stpos = QPoint(0, 0)
def __init__(self):
super().__init__()
screen = QApplication.primaryScreen()
self.originalPixmap = screen.grabWindow(
QApplication.desktop().winId()
)
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setPen(Qt.NoPen)
rectSize = QApplication.desktop().screenGeometry()
painter.drawPixmap(rectSize, self.originalPixmap)
painterPath = QPainterPath()
painterPath.addRect(QRectF(rectSize))
painterPath.addRoundedRect(QRectF(self.stpos, self.endpos), 0, 0)
painter.setBrush(QBrush(QColor(0, 0, 100, 100)))
painter.drawPath(painterPath)
painter.end()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
def mouseMoveEvent(self, event):
self.endpos = event.pos()
self.repaint()
def mousePressEvent(self, event):
self.stpos = event.pos()
def mouseReleaseEvent(self, event):
self.endpos = event.pos()
self.screenShot()
def screenShot(self):
pmap = self.originalPixmap.copy(QRect(self.stpos, self.endpos))
filename, _ = QFileDialog.getSaveFileName(self)
if len(filename) == 0:
return
filename = str(Path(filename).with_suffix(".png"))
pmap.save(filename)
self.close()
return MainWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())