Skip to content

Instantly share code, notes, and snippets.

@ethanrublee
Last active September 15, 2020 00:13
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 ethanrublee/fb86ebe4fc37700e7f902baf351d413b to your computer and use it in GitHub Desktop.
Save ethanrublee/fb86ebe4fc37700e7f902baf351d413b to your computer and use it in GitHub Desktop.
Teaching the kids how to code and make stop motion animations!
import numpy as np
import cv2
import argparse
import os
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument('dev', help='video device')
parser.add_argument('out', help='output folder')
args = parser.parse_args()
cap = cv2.VideoCapture(args.dev)
frame_count=0
last_saved_frame = None
os.makedirs(args.out)
cv2.namedWindow('frame', 0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if frame is None:
continue
if last_saved_frame is None:
last_saved_frame = frame
display = frame * 0.75 + last_saved_frame*0.25
# Display the resulting frame
cv2.imshow('frame', display/255)
key = cv2.waitKey(10) & 0xFF
if key == ord(' '):
frame_count+=1
last_saved_frame = frame[:]
out_file=os.path.join(args.out,'%05d.png'%frame_count)
print('saving %s'%out_file)
cv2.imwrite(out_file, frame)
elif key == ord('s'):
cmd="convert -delay 15 %s/*.png +dither +map %s.gif"%(args.out, args.out)
print(cmd)
subprocess.check_call(cmd, shell=True)
elif key != 255:
print('you pressed %s'%chr(key))
if key == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
@ethanrublee
Copy link
Author

Our first animations, made with convert:

convert -delay 5 *.png joules_first.gif 

wylders_first
joules_first
gif_of_my_images

@ethanrublee
Copy link
Author

Now it writes out the gif while you're working. Convenient to open up the gif and watch it while you're animating.

Magnets on a white board by Dad:
whiteboard01
Portal by Joule:
portal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment