forked from CodeChix-OpenSource/PiDoorbell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiphoto.py
More file actions
77 lines (63 loc) · 2.49 KB
/
piphoto.py
File metadata and controls
77 lines (63 loc) · 2.49 KB
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
#!/usr/bin/env python
# Copyright (C) 2014 Akkana Peck <akkana@shallowsky.com>
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Take a still photo. If a USB camera is attached (/dev/video0), use it,
# else if a PiCam is attached (/dev/fb0), use that instead,
# else throw an exception.
#
import os
from subprocess import call
import time
def take_still(outfile='/tmp/still.jpg', res=[640, 480], verbose=False):
# Do we have a USB camera for which we can use fswebcam?
if os.path.exists('/dev/video0'):
if verbose:
print "Taking photo with fswebcam ..."
rv = call(['/usr/bin/fswebcam', '-d', '/dev/video0',
'-r', '%dx%d' % tuple(res), outfile])
if not rv:
return
print "fswebcam failed! Error code %d" % rv
# No luck with a USB camera. Is there a Pi camera?
if not os.path.exists('/dev/fb0'):
raise SystemError, "Can't find either a USB camera or a Pi camera!"
# Can we use the picamera module?
try:
import picamera
except ImportError:
# picamera isn't installed. Can we use raspistill?
if not os.path.exists('/usr/bin/raspistill'):
raise SystemError, \
"Neither python-picamera nor raspistill is installed"
if verbose:
print "Taking photo with raspistill"
rv = call(['/usr/bin/raspistill', '-o', outfile])
if rv:
raise "raspistill exited with %d" % rv
return
if verbose:
print "Taking photo with picamera"
with picamera.PiCamera() as camera:
camera.resolution = res
camera.start_preview()
# Camera warm-up time
time.sleep(2)
camera.capture(outfile)
# Is this needed? What does previewing mean?
camera.stop_preview()
if __name__ == '__main__':
take_photo(verbose=True)