Added an idle state and a take_picture routine
This commit is contained in:
@@ -19,10 +19,33 @@ image_size = (640, 480)
|
|||||||
# Display offset for pictures
|
# Display offset for pictures
|
||||||
image_offset = (80,60)
|
image_offset = (80,60)
|
||||||
|
|
||||||
|
# Idle image
|
||||||
|
image_idle = "idle.jpg"
|
||||||
|
|
||||||
|
# Image basename
|
||||||
|
image_basename = "pic"
|
||||||
|
|
||||||
###############
|
###############
|
||||||
### Classes ###
|
### Classes ###
|
||||||
###############
|
###############
|
||||||
|
|
||||||
|
class Images:
|
||||||
|
"""Class to manage images and count them"""
|
||||||
|
def __init__(self, basename):
|
||||||
|
self.basename = basename
|
||||||
|
self.counter = 0
|
||||||
|
self.suffix = ".jpg"
|
||||||
|
|
||||||
|
def get(self, count):
|
||||||
|
return self.basename + str(count).zfill(5) + self.suffix
|
||||||
|
|
||||||
|
def get_last(self):
|
||||||
|
return self.get(self.counter)
|
||||||
|
|
||||||
|
def get_next(self):
|
||||||
|
self.counter += 1
|
||||||
|
return self.get(self.counter)
|
||||||
|
|
||||||
class GUI_PyGame:
|
class GUI_PyGame:
|
||||||
"""The GUI class using PyGame"""
|
"""The GUI class using PyGame"""
|
||||||
def __init__(self, name, size):
|
def __init__(self, name, size):
|
||||||
@@ -47,12 +70,12 @@ class GUI_PyGame:
|
|||||||
self.screen.blit(image, offset)
|
self.screen.blit(image, offset)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def mainloop(self, actions):
|
def mainloop(self, filename):
|
||||||
while True:
|
while True:
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT: return
|
if event.type == pygame.QUIT: return
|
||||||
elif event.type == pygame.KEYDOWN: handle_keypress(event.key)
|
elif event.type == pygame.KEYDOWN: handle_keypress(event.key)
|
||||||
actions()
|
self.show_picture(filename)
|
||||||
|
|
||||||
def teardown(self):
|
def teardown(self):
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
@@ -60,12 +83,22 @@ class GUI_PyGame:
|
|||||||
class Camera:
|
class Camera:
|
||||||
"""Camera class providing functionality to take pictures"""
|
"""Camera class providing functionality to take pictures"""
|
||||||
#def __init__(self):
|
#def __init__(self):
|
||||||
def preview(self, filename="/tmp/preview.jpg"):
|
def call_gphoto(self, action, filename):
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output("gphoto2 --force-overwrite --capture-preview --quiet --filename " + filename, shell=True, stderr=subprocess.STDOUT)
|
output = subprocess.check_output("gphoto2 --force-overwrite --quiet "
|
||||||
|
+ action + " --filename " + filename,
|
||||||
|
shell=True, stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
error("Error during preview when calling '" + e.cmd + "'!\nOutput: " + e.output, e.returncode)
|
error("Error during preview when calling '" + e.cmd + "'!\nOutput: "
|
||||||
|
+ e.output, e.returncode)
|
||||||
if "ERROR" in output: error("Error during preview!\n" + output)
|
if "ERROR" in output: error("Error during preview!\n" + output)
|
||||||
|
|
||||||
|
def preview(self, filename="/tmp/preview.jpg"):
|
||||||
|
self.call_gphoto("--capture-preview", filename)
|
||||||
|
return filename
|
||||||
|
|
||||||
|
def take_picture(self, filename="/tmp/picture.jpg"):
|
||||||
|
self.call_gphoto("--capture-image-and-download", filename)
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
|
|
||||||
@@ -73,10 +106,6 @@ class Camera:
|
|||||||
### Functions ###
|
### Functions ###
|
||||||
#################
|
#################
|
||||||
|
|
||||||
def actions():
|
|
||||||
display.show_picture(camera.preview(), image_size, image_offset)
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
def error(msg, exit_code=1):
|
def error(msg, exit_code=1):
|
||||||
print "ERROR: " + msg
|
print "ERROR: " + msg
|
||||||
teardown(exit_code)
|
teardown(exit_code)
|
||||||
@@ -89,10 +118,14 @@ def handle_keypress(key):
|
|||||||
if key == ord('q'):
|
if key == ord('q'):
|
||||||
teardown()
|
teardown()
|
||||||
elif key == ord('c'):
|
elif key == ord('c'):
|
||||||
print "Taking picture"
|
print "Taking 3 pictures"
|
||||||
|
for x in xrange(3):
|
||||||
|
filename = camera.take_picture(images.get_next())
|
||||||
|
display.show_picture(filename)
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
display.mainloop(actions)
|
display.mainloop(image_idle)
|
||||||
display.teardown()
|
display.teardown()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@@ -102,6 +135,7 @@ def main():
|
|||||||
|
|
||||||
display = GUI_PyGame('Photobooth', display_size)
|
display = GUI_PyGame('Photobooth', display_size)
|
||||||
camera = Camera()
|
camera = Camera()
|
||||||
|
images = Images(image_basename)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user