def moveTeraStitcherStackToFileList(source, sink, deleteDirectory = True, verbose = True): """Moves image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ fns = glob.glob(os.path.join(source, '*/*/*')); fns = natsort.natsorted(fns); io.createDirectory(sink); for i,f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i); if verbose: print '%s -> %s' % (f,fn) shutil.move(f, fn); if deleteDirectory: p,_ = os.path.split(fns[0]); p = p.split(os.path.sep); p = p[:-2]; p = os.path.sep.join(p); shutil.rmtree(p); return sink;
def copyTeraStitcherStackToFileList(source, sink, verbose = True): """Copies image files from TeraSticher file structure to a list of files Arguments: source (str): base directory of the TeraStitcher files sink (str): regular expression of the files to copy to verbose (bool): show progress Returns: str: sink regular expression """ #TODO: multiple tiles ! fns = glob.glob(os.path.join(source, '*/*/*')); fns = natsort.natsorted(fns); #print fns io.createDirectory(sink); for i,f in enumerate(fns): fn = filelist.fileExpressionToFileName(sink, i); if verbose: print '%s -> %s' % (f,fn) shutil.copyfile(f, fn); return sink;
def stitchData(xmlPlacementFile, resultPath, algorithm = None, resolutions = None, form = None, channel = None, subRegion = None, bitDepth = None, blockSize = None, cleanup = True, compress = False): """Runs the final stiching step of TeraSticher Arguments: xmlPlacementFile (str or None): the xml placement descriptor resultPath (str): result path, file name or file expression for the stiched data algorithm (str or None): optional algorithm to use for placement: 'NOBLEND' for no blending 'SINBLEND' for sinusoidal blending resolutions (tuple or None): the different resolutions to produce form (str or None): the output form, if None determined automatically channel (str or None): the channels to use, 'R', 'G', 'B' or 'all' subRegion (tuple or None): optional sub region in the form ((xmin,xmax),(ymin,ymax), (zmin, zmax)) bitDepth (int or None): the pits per pixel to use, default is 8 blockSize (tuple): the sizes of various blocks to save stiched image into cleanup (bool): if True delete the TeraSticher file structure compress (bool): if True compress final tif images Returns: str : the result path or file name of the stiched data See also: `TeraStitcher project step <https://github.com/abria/TeraStitcher/wiki/Step-6:-Merge>`_. """ checkSticherInitialized(); global TeraStitcherBinary; cmd = TeraStitcherBinary + ' --merge --imout_format="tif" '; cmd = cmd + '--projin="' + xmlPlacementFile + '" '; if len(resultPath) > 3 and resultPath[-4:] == '.tif': if io.isFileExpression(resultPath, check = False): form = 'TiledXY|2Dseries'; resultPath, filename = os.path.split(resultPath); else: form = 'TiledXY|3Dseries'; resultPath, filename = os.path.split(resultPath); else: filename = None; cmd = cmd + '--volout="' + resultPath + '" '; if algorithm is not None: cmd = cmd + ' --algorithm="' + algorithm + '" '; if resolutions is not None: cmd = cmd + '--resolutions="' for r in sorted(resolutions): cmd = cmd + str(r); cmd = cmd + ' '; if form is not None: cmd = cmd + '--volout_plugin="' + form + '" '; if channel is not None: cmd = cmd + '--imin_channel="' + channel + '" '; if subRegion is not None: sns = (('--R0=', '--R1='), ('--C0=', '--C1='), ('--D0=', '--D1-')); for d in range(3): for m in range(2): if subRegion[d][m] is not None: cmd = cmd + sns[d][m] + str(subRegion[d][m]) + ' '; if blockSize is not None: bs = ('--slicewidth=', '--sliceheight=', '--slicedepth='); for d in range(3): if blockSize[d] is not None: cmd = cmd + bs[d] + str(blockSize[d]) + ' '; if bitDepth is not None: cmd = cmd + '--imout_depth=' + str(bitDepth) + ' '; if not compress: cmd = cmd + '--libtiff_uncompress '; #print resultPath io.createDirectory(resultPath, split = False) print 'running: ' + cmd; res = os.system(cmd); if res != 0: raise RuntimeError('stitchData: failed executing: ' + cmd); if filename is not None: if io.isFileExpression(filename, check = False): # convert list of files in TeraSticher from #TODO: multiple resolutions basedir = max(glob.glob(os.path.join(resultPath, '*')), key = os.path.getmtime); if cleanup: moveTeraStitcherStackToFileList(basedir, os.path.join(resultPath, filename), deleteDirectory=True); #shutil.rmtree(basedir); else: copyTeraStitcherStackToFileList(basedir, os.path.join(resultPath, filename)); else: # single file in TeraSticher folder #get most recent created file #TODO: test if this works imgfile = max(glob.glob(os.path.join(resultPath, '*/*/*/*')), key = os.path.getmtime); filename = os.path.join(resultPath, filename); os.rename(imgfile, filename); if cleanup: imgpath = os.path.sep.join(imgfile.split(os.path.sep)[:-3]); shutil.rmtree(imgpath) return filename; else: return resultPath;