-
Notifications
You must be signed in to change notification settings - Fork 2
/
screenshot.py
62 lines (52 loc) · 2.13 KB
/
screenshot.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
import sys
import argparse
from PyQt4 import QtGui
from PyQt4 import QtCore
app = QtGui.QApplication(sys.argv)
class ScreenShot(QtGui.QWidget):
def __init__(self):
super(ScreenShot, self).__init__()
self.outPath = None
screen_rect = app.desktop().screenGeometry()
width, height = screen_rect.width()*2, screen_rect.height()
self.setGeometry(0, 0, width, height)
self.setWindowTitle('Screen Capture')
self.setCursor(QtCore.Qt.CrossCursor)
self.setWindowOpacity(0.1)
self.rubberband = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self)
self.setMouseTracking(True)
def mousePressEvent(self, event):
self.origin = event.pos()
self.rubberband.setGeometry(QtCore.QRect(self.origin, QtCore.QSize()))
self.rubberband.show()
QtGui.QWidget.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.setGeometry(
QtCore.QRect(self.origin, event.pos()).normalized())
QtGui.QWidget.mouseMoveEvent(self, event)
def mouseReleaseEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.hide()
selected = []
rect = self.rubberband.geometry()
desktop = QtGui.QApplication.instance().desktop()
imgmap = QtGui.QPixmap.grabWindow(desktop.winId(), rect.x(), rect.y(), rect.width(), rect.height())
imgmap.save(self.outPath)
sys.exit()
QtGui.QWidget.mouseReleaseEvent(self, event)
def launch(self, imagePath):
self.outPath = imagePath
self.show()
def captureRegion(outImagePath):
app = QtGui.QApplication(sys.argv)
lch = ScreenShot()
lch.launch(outImagePath)
sys.exit(app.exec_())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Simple tool to take screenshot can be used as a module also')
parser.add_argument('-o','--out', help='output path for image to be saved', required=True)
args = parser.parse_args()
lch = ScreenShot()
lch.launch(args.out)
sys.exit(app.exec_())