-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshot.py
87 lines (62 loc) · 1.94 KB
/
Snapshot.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
'''
Code from https://gist.github.com/snim2/255151
Additions by Eric Evans
'''
import pygame
import pygame.camera
import time
import os
from pygame.locals import *
DEVICE_STUB = '/dev/video'
DEV_BASE = 1
SIZE = (640, 480)
DIR = './captures'
class Snapshot(object):
display = None
camera = None
def __init__(self):
pygame.init()
pygame.camera.init()
def _get_image(self, screen):
return self.camera.get_image(screen)
def capture(self):
screen = self._start()
capture = True
while capture:
screen = self._get_image(screen)
self.display.blit(screen, (0,0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == QUIT:
capture = False
elif event.type == KEYDOWN and (event.key == K_s or event.key == K_SPACE):
path = self._save(screen)
capture = False
self._stop()
return path
def _start(self):
self.display = pygame.display.set_mode(SIZE, 0)
pygame.display.set_caption('Scanning Screen')
i = DEV_BASE
again = True
while again:
try:
device = '{}{}'.format(DEVICE_STUB,i)
print('trying {}'.format(device))
self.camera = pygame.camera.Camera(device, SIZE)
self.camera.start()
again = False
except:
i = i + 1
return pygame.surface.Surface(SIZE, 0, self.display)
def _save(self, img, filename=str(int(time.time()))):
filename = '{}.png'.format(filename)
path = os.path.join(DIR, filename)
pygame.image.save(img, path)
return path
def _stop(self):
self.camera.stop()
pygame.quit()
if __name__ == '__main__':
s = Snapshot()
s.capture()