-
Notifications
You must be signed in to change notification settings - Fork 23
/
sgh_RasPiCamera.py
executable file
·71 lines (50 loc) · 2.29 KB
/
sgh_RasPiCamera.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
#!/usr/bin/env python
# sgh_RasPiCamera - control the raspberry pi camera via scratch
#Copyright (C) 2014 by Matt Venn
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Version = '0.0.1sw5' # 22May14 Add Try except arroudn take pic code
import os,glob,pwd,grp
class RasPiCamera:
def __init__(self):
print "pi camera init"
self.num = 0
self.user = os.getenv("SUDO_USER")
self.dir = ("/home/"+ self.user + "/photos/")
try:
os.mkdir(self.dir)
except OSError:
#already exists
pass
photos = glob.glob(self.dir + '*.jpg')
if len(photos):
latest_photo = sorted(photos)[-1]
latest_photo = latest_photo.replace(self.dir,'')
latest_photo = latest_photo.replace('.jpg','')
latest_photo_num = int(latest_photo)
self.num = latest_photo_num + 1
print("starting at %d" % self.num)
def take_photo(self):
try:
photo_file = self.dir + str(self.num) + '.jpg'
os.system("raspistill -n -t 1 -o " + photo_file)
#chown the photo so it can be modified by the user
uid = pwd.getpwnam(self.user).pw_uid
#assume group is same name as user
gid = grp.getgrnam(self.user).gr_gid
os.chown(photo_file, uid, gid)
print "photo taken: " + photo_file
self.num += 1
except:
print "Error taking photo - camera probably not correctly fitted"
pass
#### end RasPiCamera ###############################################################