def stack_to_tif(stack_path, compress=1, wipe=False): """ :param stack_path: string, full path of .stack file to be converted to .tif :param compress: int, compression level to use for tif file :param wipe: bool, if True stack file will be deleted after saving as tif :return: """ from numpy import array_equal from os import remove from os.path import split, sep from numpy import fromfile import volTools as volt from skimage.external import tifffile as tif dims = volt.getStackDims(split(stack_path)[0] + sep) im = fromfile(stack_path, dtype='int16') im = im.reshape(dims[-1::-1]) tif_path = stack_path.split('.')[0] + '.tif' tif.imsave(tif_path, im, compress=compress) if wipe: check_file = tif.imread(tif_path) if array_equal(check_file, im): print('Deleting {0}...'.format(stack_path)) remove(stack_path) else: print('{0} and {1} differ... something went wrong!'.format(stack_path, tif_path))
def stack_loader(stack_path): import volTools as volt from numpy import fromfile from os.path import sep, split dims = volt.getStackDims(split(stack_path)[0] + sep) im = fromfile(stack_path, dtype='int16') im = im.reshape(dims[-1::-1]) return im
def tToZStacks(inDir): ''' tToZStacks(inDir): Writes the tStacks in 'inDir' to zStacks in 'inDir\\zStacks' ''' import sys, os sys.path.insert(0, 'C:/Users/pujalaa/Documents/Code/Python/code/codeFromNV') sys.path.insert(0, 'C:/Users/pujalaa/Documents/Code/Python/code/util') import time import numpy as np import volTools imgDir = inDir # allFilesInDir = os.listdir(imgDir) fileExt = '.stack' fileStem = 'Plane' #tStacks = np.sort(list(filter(lambda x: x.startswith(fileStem),\ # allFilesInDir))) stackDims = volTools.getStackDims(imgDir) nTimePts = volTools.getNumTimePoints(imgDir) outDir = os.path.join(imgDir,'zStacks') if os.path.isdir(outDir): print(outDir, '\n ...already exists!') else: os.mkdir(outDir) print('Writing files...') startTime = time.time() for stackNum in range(stackDims[2]): print('Reading plane ' + str(stackNum+1) + '...') fn = fileStem + '%0.2d' % (stackNum+1) + fileExt fp = os.path.join(imgDir,fn) blah = np.fromfile(fp,dtype = 'uint16') blah = np.reshape(blah,(nTimePts,stackDims[1], stackDims[0])) print('Writing plane ' + str(stackNum+1) + '...') for timePt in range(nTimePts): fn = 'TM' + '%0.05d' % timePt + '.bin' fp = os.path.join(outDir,fn) file = open(fp,'ab') file.write(blah[timePt]) file.close() print(int(time.time()-startTime)/60,'min')
def stack_to_tif(stack_path): """ :param stack_path: string, full path of .stack file to be converted to .tif :return: """ from os.path import split, sep from numpy import fromfile import volTools as volt from skimage.external import tifffile as tif dims = volt.getStackDims(split(stack_path)[0] + sep) im = fromfile(stack_path, dtype='int16') im = im.reshape(dims[-1::-1]) tif_path = stack_path.split('.')[0] + '.tif' tif.imsave(tif_path, im, compress=1)