Skip to content

Instantly share code, notes, and snippets.

@jeffThompson
Last active November 30, 2015 05:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffThompson/96bc286fc2ba11128d37 to your computer and use it in GitHub Desktop.
Save jeffThompson/96bc286fc2ba11128d37 to your computer and use it in GitHub Desktop.
A dumb little thing that makes an animated GIF from your Makerbot preview window. Start at Layer 0, run this script, and out comes a lovely GIF.
'''
MAKERBOT LAYERS >> GIF
Jeff Thompson | 2015 | www.jeffreythompson.org
A dumb little thing that makes an animated GIF from your Makerbot preview
window. Start at Layer 0, run this script, and out comes a lovely GIF.
EXAMPLE OUTPUT:
This sweet saber-toothed tiger skull:
http://www.jeffreythompson.org/downloads/SaberToothedSkullAnimation_300px.gif
SAMPLE USAGE:
python makerbotlayersgif.py --frames 166 --bbox 422,69,1017,572 --width 300
BUILD THE OBJECT TOP DOWN?
Key code 125 is the down arrow in AppleScript:
cmd = """osascript -e 'tell application "System Events" to key code 125'"""
REQUIREMENTS and THANK YOU:
http://pyscreenshot.readthedocs.org/en/latest/
http://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
'''
import sys, os, time, subprocess, shutil, argparse
import pyscreenshot as ImageGrab
ALL_OFF = '\033[0m'
BOLD = '\033[1m'
REVERSE = '\033[7m'
# parse arguments
parser = argparse.ArgumentParser(description='Create a sweet animated gif from the Makerbot preview.')
parser.add_argument('-f', '--frames', type=int, required=True, help='Number of frames to capture (listed as "layers" in the Makerbot app).')
parser.add_argument('-b', '--bbox', required=False, help='Bounding box for capture, listed as x1,y1,x2,y2.')
parser.add_argument('-w', '--width', required=False, help='Width of gif output (in pixels).')
args = vars(parser.parse_args())
num_layers = args['frames']
if args['bbox'] != None:
b = args['bbox'].split(',')
bbox = (int(b[0]), int(b[1]), int(b[2]), int(b[3]))
# create a temporary 'frames' folder
try:
os.mkdir('frames')
except:
pass
# countdown
print BOLD + 'MAKERBOT LAYERS GIFMAKER' + ALL_OFF
print REVERSE + '[ select Makerbot window now ]' + ALL_OFF
print 'Capturing in',
for i in range(5, 0, -1):
print str(i) + '...',
sys.stdout.flush()
time.sleep(1)
print '\n' + BOLD + 'STARTING!' + ALL_OFF
# grab it!
for i in range(num_layers):
print '- ' + str(i+1) + '/' + str(num_layers)
# take a screenshot
filename = 'frames/' + ('%03d' % (i, )) + '.png'
if bbox == None:
im = ImageGrab.grab()
else:
im = ImageGrab.grab(bbox=bbox)
im.save(filename)
# down key
cmd = """osascript -e 'tell application "System Events" to key code 126'"""
os.system(cmd)
# build into animation
print 'Building animation...'
if args['width'] != None:
p = subprocess.call([ 'convert', '-delay', '10', '-loop', '1', '-resize', args['width'] + 'x' + args['width'], 'frames/*.png', 'layers.gif'])
else:
p = subprocess.call([ 'convert', '-delay', '10', '-loop', '1', 'frames/*.png', 'layers.gif'])
# delete temp directory
print 'Deleting temp directory...'
shutil.rmtree('frames')
# all done!
print BOLD + 'DONE!' + ALL_OFF + '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment