from pylsm import lsmreader
import sys, string, os
import matplotlib.pyplot as plt
from libtiff import TIFFimage


def main(filename):
    fid = open(filename,'r')
    files = map(string.rstrip,fid.readlines())
    fid.close()
    for f in files:
        imageFile = lsmreader.Lsmimage(f)
        imageFile.open()
        
        imageData = []
        cnt = 0
        while 1:
            try:
                imageData.append(imageFile.get_image(stack=cnt,channel=0))
                cnt += 1
            except:
                break
        outdir = string.replace(filename,'.cfg','')
        if not os.path.exists(outdir):
            os.makedirs(outdir)
        cnt = 0
        for i in imageData:
            fcnt = "%03d" % (cnt,)
            tif = TIFFimage(i)
            tif.write_file(outdir+'/'+outdir+fcnt+'.tif',compression='none')
            cnt += 1
            del tif
    
if __name__ == "__main__":
    main(sys.argv[-1])

