#!/usr/bin/python

import re
import sys
import os
import subprocess

ls = os.listdir( sys.argv[1] )

match = re.compile("aln(_[0-9]_)?[0-9]+\.png")

proc = dict()

for i in ls:
    if( match.match( i ) ):
        file = i
        i = re.sub("^[^0-9]*", "", i )
        i = re.sub("[^0-9]*$", "", i )
        p = re.split("[a-z_.]+", i )
        sid = p[ len(p)-1 ]
        if( not (sid in proc ) ):
            proc[sid] = ['', '']
        if( p[0] == '3' ):
            proc[sid][0] = file
        else:
            proc[sid][1] = file

for k in proc.keys():
    i = proc[k];
    ofn = os.path.join( sys.argv[2], k+".png" )
    ofn1 = os.path.join( sys.argv[2], k+".jpg" )
    cmd = "convert -size 2266x1154 xc:black "+i[0]
    cmd = cmd + " -geometry +0+0 -composite "+i[1]
    cmd = cmd + " -geometry +0+640 -composite "
    cmd = cmd + ofn
    print cmd
    myp = subprocess.Popen( cmd, shell=True )
    myp.wait()
    cmd = "convert -resize 256 "+ofn+" "+ofn1
    print cmd
    myp = subprocess.Popen( cmd, shell=True )
    myp.wait()
