From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search
from argparse import ArgumentParser
from glob import glob
from PIL import Image
from cStringIO import StringIO

p = ArgumentParser("turns images into a movie")
p.add_argument("input", nargs="+", default=[])
p.add_argument("--width", type=int, default=640, help="the output width in PIL")
p.add_argument("--height", type=int, default=480, help="the output height in PIL")
p.add_argument("--color", default="black", help="the output color in PIL")
p.add_argument("--output", default="frames.mp4", help="the output movie")
args = p.parse_args()

ff = args.input # glob("*.jpeg")

#print ff, args.width, args.height
#import sys 
of = open("tmp.mjpeg", "wb")
s = []
#print ff
for p in ff:
        if p.lower().endswith(".jpeg") or p.endswith(".jpg") or p.endswith(".png") or p.endswith(".gif"):
                nf ="RGB", (args.width, args.height), color=args.color)
                im =
                im.thumbnail((args.width, args.height))
                iw, ih = im.size
                # paste im onto nf
                ix = args.width/2 - iw/2
                iy = args.height/2 - ih/2
                nf.paste(im, (ix,iy))
                buf = StringIO()
      , format="jpeg")

import os
os.system("ffmpeg -i tmp.mjpeg \""+args.output+"\" -y")        
print "done"

#        buf = StringIO()
#, format="jpeg")
#        of.write(buf.getvalue())