Making animated gifs from Vpython

From GeoMod

Jump to: navigation, search

A function that takes snapshot of the screen, and saves it to a file.

The function takes a number (fnum), and uses it to name the file (e.g. img-001.png).

import subprocess

def take_screenshot(fnum):
    fnum = str(fnum).zfill(3)
    fname = "img-"+fnum+".png"
    #print fname
    cmd = ["screencapture", "-S", fname]
    test = subprocess.Popen( cmd, stdout=subprocess.PIPE).communicate()[0]

to crop the snapshots you can use Gimp or, if you can figure out the size of the window you want cropped (e.g. 500x225), and the x and y locations of the top left edge (e.g. +0+45) you can use:

for i in img-*.png
do 
   outn="c-"$i
   convert $i -crop 500x225+0+45 $outn
done

to create an animated gif use the command:

> convert c-img-*.png image.gif

to set the speed of the animation (e.g. to one frame every 25/100 seconds) use:

> gifsicle -b -d25 image.gif

To create a movie (mp4) from the images (with bit rate 1800 and a frame rate of 10) use:

> ffmpeg -r 10 -b 1800 -i c-img-%03d.png image-1800.mp4
Personal tools