Skip to content

Instantly share code, notes, and snippets.

@russb78
Last active December 28, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russb78/7412501 to your computer and use it in GitHub Desktop.
Save russb78/7412501 to your computer and use it in GitHub Desktop.
"""
Raspberry Pi Stop Motion Animation prototype.
You *may* need to set your RasPi to 1280x720 for this to work properly.
You will need to create a folder for the project in 'pi' called 'sto_mo'
and place a seperate folder called 'pics' inside it.
Press the space bar to take a picture.
Press the 'o' key to go into Onion Skinning mode (and press it again to switch back).
Press the delete key to remove the last picture
Press the enter key to quit the app and make a movie of your pictures.
Press 'q' to quit.
Thanks for testing...
"""
import pygame, os, sys
from pygame.locals import *
import picamera
#define global variables
num = 0
pics_list = []
fps = 5
# Initialise everything
pygame.init()
screen = pygame.display.set_mode([1280, 720])
pygame.display.toggle_fullscreen()
pygame.display.set_caption("Pi-Mo v0.3")
camera = picamera.PiCamera()
camera.resolution = camera.MAX_IMAGE_RESOLUTION
play_clock = pygame.time.Clock()
# set preview and resolution to full to get it to work.
def take_pic():
""" Grab an initial image and load it. One version is transformed
for preview, the other is halfed in size for the final saved image"""
global num, preview_pic
camera.capture('/home/pi/sto_mo/pics/image_' + str(num) + '.jpg')
pre_pic = pygame.image.load(os.path.join('pics', 'image_' + str(num) + '.jpg'))
preview_pic = pygame.transform.scale(pre_pic, (960, 720))
finished_pic = pygame.transform.scale(pre_pic, (1296, 972))
pygame.image.save(finished_pic, '/home/pi/sto_mo/pics/transformed_' + str(num) + '.jpg')
num += 1
print num, " pictures taken"
def delete_pic():
"""Doesn't actually delete the last picture, but it will be
successfully overwritten the next time you take a shot"""
global num, preview_pic
num -= 1
preview_pic = pygame.image.load(os.path.join('pics', 'image_' + str(num) + '.jpg'))
print "Last picture deleted!"
def make_movie():
"""Quit out of the application and create a movie with your taken shots"""
camera.stop_preview()
print "Quitting Sto-Mo to create your video. Come again!\n"
pygame.quit()
os.system("avconv -r " + str(fps) + " -i '/home/pi/sto_mo/pics/image_%d.jpg' -vcodec libx264 video.mp4")
sys.exit(0)
def alpha_preview():
"""Make the preview semi-transparent.
You have to stop preview first for it to work properly"""
camera.stop_preview()
camera.preview_alpha = 128
camera.start_preview()
def standard_preview():
"""Return preview to solid optimacy.
You have to stop preview first for it to work properly"""
camera.stop_preview()
camera.preview_alpha = 255
camera.start_preview()
def update_display():
"""Blit the screen (behind the camera preview) with the last picture.
It has already been transformed to fit as part of 'take_pic()' """
global curr_input
screen.fill((0,0,0))
if num > 0:
screen.blit(preview_pic, (160, 0))
pygame.display.update()
next_state = alpha_preview
current_state = standard_preview
standard_preview()
while True:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
camera.close()
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
take_pic()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_BACKSPACE:
delete_pic()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
make_movie()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_q:
camera.close()
pygame.quit()
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_o:
next_state()
current_state, next_state = next_state, current_state
update_display()
except KeyboardInterrupt:
camera.close()
pygame.quit()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment