Skip to content

Instantly share code, notes, and snippets.

@lukegb
Created May 12, 2012 21:52
Show Gist options
  • Save lukegb/2669305 to your computer and use it in GitHub Desktop.
Save lukegb/2669305 to your computer and use it in GitHub Desktop.
import glob, sys, os.path
from PIL import Image
def getMaxFrame(folder_a, folder_b, fileext):
fin_a = glob.glob(os.path.join(folder_a, "*.%s" % (fileext,)))
fin_b = glob.glob(os.path.join(folder_b, "*.%s" % (fileext,)))
return min(len(fin_a), len(fin_b))
def checkAllFramesExist(folder_a, folder_b, fileext, max_count):
for i in range(1, max_count + 1):
path_a = os.path.join(folder_a, "%04d.%s" % (i, fileext))
path_b = os.path.join(folder_b, "%04d.%s" % (i, fileext))
if not os.path.exists(path_a):
print "Error: %s does not exist!" % (path_a,)
sys.exit(2)
elif not os.path.exists(path_b):
print "Error: %s does not exist!" % (path_b,)
sys.exit(2)
def createOutputFrames(folder_a, folder_b, fileext, max_count, output):
for i in range(1, max_count + 1):
current_filename = "%04d.%s" % (i, fileext)
path_a = os.path.join(folder_a, current_filename)
path_b = os.path.join(folder_b, current_filename)
doProgressBar(i-1, max_count)
img_l = Image.open(path_a)
img_r = Image.open(path_b)
assert img_l.mode == img_r.mode
assert img_l.size == img_r.size
width, height = img_l.size
img_o = Image.new(img_l.mode, (width*2, height), None)
img_o.paste(img_l, (0, 0))
img_o.paste(img_r, (width, 0))
img_o.save(os.path.join(output, current_filename))
doProgressBar(max_count, max_count)
def doProgressBar(currentFile, maxFile):
consoleLength = 60
currentFile = float(currentFile)
maxFile = float(maxFile)
equalsSigns = '=' * int(round(currentFile / maxFile * consoleLength))
spaces = ' ' * int(round(consoleLength - len(equalsSigns)))
print "\r[%s%s] (%d / %d: %d%%)" % (equalsSigns, spaces, currentFile, maxFile, round(currentFile/maxFile*100)),
if __name__ == "__main__":
if len(sys.argv) != 5:
print "%s <input dir - left> <input dir - right> <output dir> <file extension - no .>" % (sys.argv[0],)
sys.exit(1)
inp_l, inp_r, out, file_ext = sys.argv[1:]
if not os.path.exists(inp_l):
print inp_l, "does not exist!"
sys.exit(3)
if not os.path.exists(inp_r):
print inp_r, "does not exist!"
sys.exit(3)
if not os.path.exists(out):
print out, "does not exist!"
sys.exit(3)
maxFrame = getMaxFrame(inp_l, inp_r, file_ext)
if maxFrame == 0:
print "Err, there aren't any frames."
sys.exit(4)
checkAllFramesExist(inp_l, inp_r, file_ext, maxFrame)
createOutputFrames(inp_l, inp_r, file_ext, maxFrame, out)
print "ffmpeg -i %04d.png -r 30 out.mp4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment