Janky Lego Stop Motion

Well the kids have lost interest in Raspberry Pi Python programming for now, but look who’s still at it! The jankyiest of Lego stop motions.

Here was the code I tossed together to make the gif above:

#!/usr/bin/env python2

import os
import time
import shutil
import datetime
import tempfile
import pygame.camera
import pygame.image
import RPi.GPIO as GPIO

save_dir = '/usr/share/nginx/www'

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setup(17, GPIO.IN)

pygame.camera.init()
camera = pygame.camera.Camera('/dev/video0')

def make_picture(filename):
    raw_input('Ready for picture? ')
    camera.start()
    image = camera.get_image()
    pygame.image.save(image, filename)
    camera.stop()

def make_gif(frames=5):
    print 'Making you a gif using %s frames, get ready!' % frames
    time.sleep(0.5)
    dir = tempfile.mkdtemp()
    for i in range(frames):
        print 'Taking picture!'
        make_picture('%s/%s.jpg' % (dir, i))
        time.sleep(3)

    print 'Converting images to gif, please wait...'
    os.system('convert -delay 20 %s/*.jpg %s/animated.gif' % (dir, dir))

    filename = '%s.gif' % datetime.datetime.now().isoformat()
    shutil.move('%s/animated.gif' % dir, '%s/%s' % (save_dir, filename))
    shutil.rmtree(dir)
    print 'Complete!'

while True:
  if GPIO.input(17):
      make_gif()

GPIO.cleanup()

And a picture of the rig: Screen Shot 2015-12-21 at 6.42.50 PM