annotate python/other/takeshot.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 2ef2247f8dda
children
rev   line source
302
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
2
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
3 """
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
4 Utility to take a snapshot of the screen
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
5 """
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
6
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
7 import sys
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
8 from PyQt5.QtGui import QGuiApplication, QPainter, QPen
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
9 from PyQt5.QtCore import QRect, Qt, pyqtSignal, QBuffer
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
10 from PyQt5.QtWidgets import QApplication, QWidget
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
11
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
12
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
13 class RegionSelecter(QWidget):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
14 sigBitmap = pyqtSignal(bytes)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
15 def __init__(self):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
16 super().__init__()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
17 screen = QGuiApplication.primaryScreen()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
18 self.desktopPixmap = screen.grabWindow(0)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
19 self.setWindowFlags(Qt.FramelessWindowHint | Qt.X11BypassWindowManagerHint | Qt.WindowStaysOnTopHint)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
20 self.setWindowState(Qt.WindowFullScreen)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
21 self.move(0, 0)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
22 self.resize(self.desktopPixmap.size())
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
23 self.drawSelection = False
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
24
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
25 @property
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
26 def Selection(self):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
27 return QRect(self.startPoint, self.endPoint).normalized()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
28
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
29 def paintEvent(self, ev):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
30 painter = QPainter(self)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
31 painter.drawPixmap(0, 0, self.desktopPixmap)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
32 if self.drawSelection:
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
33 painter.setPen(QPen(Qt.red))
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
34 painter.drawRect(self.Selection)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
35
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
36 def mousePressEvent(self, ev):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
37 self.startPoint = ev.pos()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
38 self.endPoint = ev.pos()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
39 self.drawSelection = True
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
40
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
41 def mouseMoveEvent(self, ev):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
42 self.update()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
43 self.endPoint = ev.pos()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
44
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
45 def mouseReleaseEvent(self, ev):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
46 self.endPoint = ev.pos()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
47 pm = self.desktopPixmap.copy(self.Selection)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
48 memfile = QBuffer()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
49 pm.save(memfile, "png")
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
50 self.sigBitmap.emit(bytes(memfile.data()))
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
51
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
52
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
53 def takeShot():
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
54 app = QApplication(sys.argv)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
55 screen = QGuiApplication.primaryScreen()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
56 desktopPixmap = screen.grabWindow(0)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
57 desktopPixmap.save('tst2.png')
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
58
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
59
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
60 def takeRegion():
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
61 """ Take a snapshot of the current desktop """
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
62 app = QApplication(sys.argv)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
63 r = RegionSelecter()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
64 TMP = {}
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
65 def handleBitmap(bm):
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
66 app.quit()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
67 TMP['bm'] = bm
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
68 r.sigBitmap.connect(handleBitmap)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
69 r.show()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
70 app.exec()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
71 return TMP['bm']
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
72
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
73
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
74 if __name__ == '__main__':
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
75 bm = takeRegion()
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
76 print(bm)
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
77 with open('tst.png', 'wb') as f:
2ef2247f8dda Added screenshot application
Windel Bouwman
parents:
diff changeset
78 f.write(bm)