Frames2movie

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search
#!/usr/bin/python
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 = Image.new("RGB", (args.width, args.height), color=args.color)
                im = Image.open(p)
                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()
                nf.save(buf, format="jpeg")
                of.write(buf.getvalue())

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

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

of.close()