def run(): srcDir = DirectoryChooser("Choose directory").getDirectory() if not srcDir: return targetDir = DirectoryChooser("Choose target directory").getDirectory() if targetDir is None: # User canceled the dialog return sId = ".tiff" for root, directories, filenames in os.walk(srcDir): for filename in filenames: path = os.path.join(root, filename) if not (sId in filename): continue cs = ChannelSeparator() cs.setId(path) print "cs", cs bf = BFVirtualStack(path, cs, False, False, False) for sliceIndex in xrange(1, bf.getSize() + 1): print "Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) sliceFileName = os.path.join(targetDir + filename + "_" + str(sliceIndex) + ".tiff") print "writing ", sliceFileName FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName)
def run(): index = 0 # Chose a file to open srcDir = DirectoryChooser("Choose directory containing sequence to split").getDirectory() # Chose a directory to store each slice as a file targetDir = DirectoryChooser("Chose folder in which to save sequence").getDirectory() if targetDir is None: # User canceled the diolog return if srcDir is None: # User canceled dialog return #path = os.path.join(srcDir, od.getFileName()) for f in os.listdir(srcDir): path = os.path.join(srcDir, f) # Ready: cs = ChannelSeparator() cs.setId(path) bf = BFVirtualStack(path, cs, False, False, False) fname = "%06d"%(index-index%200) path = os.path.join(targetDir,fname) try: os.mkdir(path) except Exception, e: print "Couldn't make directory:",e for sliceIndex in xrange(1,bf.getSize() +1): print "Well done. Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) string = "%06d.tif"%index sliceFileName = os.path.join(path, string) FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName) index +=1 print "Finished:",path
def run(FolderName, SaveFolder): # Find tiffiles in folder onlyfiles = [ f for f in os.listdir(FolderName) if os.path.isfile(os.path.join(FolderName, f)) and f.lower().endswith(".tif") ] for ii in xrange(0, len(onlyfiles)): path = os.path.join(FolderName, onlyfiles[ii]) print "Processing file..", path # Find stimulus, Block, regions from filename for sorting # Get all underscores underscores = [m.start() for m in re.finditer("_", onlyfiles[ii])] # Fish Name findfish = onlyfiles[ii].find("Fish") findunderscore = [i for i in underscores if i > findfish][0] fishnum = onlyfiles[ii][findfish:findunderscore] print "Fish Number..", fishnum # Block findblock = onlyfiles[ii].find("Block") findunderscore = [i for i in underscores if i > findblock][0] blocknum = onlyfiles[ii][findblock:findunderscore] print "Block Number..", blocknum # Stimulus findstimulus = onlyfiles[ii].find("Blue") findunderscore = [i for i in underscores if i > findstimulus][0] stimulustype = onlyfiles[ii][findstimulus:findunderscore] print "Stimulus type..", stimulustype # Region findregion = onlyfiles[ii].find("Hb") findunderscore = [i for i in underscores if i > findregion][0] region = onlyfiles[ii][findregion - 1 : findunderscore] print "Region..", region targetDir = os.path.join(SaveFolder, fishnum, region) + filesep print "Images will be saved in .....", targetDir if not os.path.exists(targetDir): os.makedirs(targetDir) cs = ChannelSeparator() cs.setId(path) bf = BFVirtualStack(path, cs, False, False, False) for sliceIndex in xrange(1, bf.getSize() + 1): print "Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) sliceFileName = os.path.join(targetDir, "T=" + str(sliceIndex) + ".tif") FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName)
def run(FolderName, SaveFolder): # Find tiffiles in folder onlyfiles = [f for f in os.listdir(FolderName) if os.path.isfile(os.path.join(FolderName, f)) and f.lower().endswith('.tif')] for ii in xrange(0, len(onlyfiles)): path = os.path.join(FolderName, onlyfiles[ii]) print "Processing file..", path # Find stimulus, Block, regions from filename for sorting # Get all underscores underscores = [m.start() for m in re.finditer('_', onlyfiles[ii])] # Fish Name findfish = onlyfiles[ii].find('Fish') findunderscore = [i for i in underscores if i > findfish][0] fishnum = onlyfiles[ii][findfish:findunderscore] print 'Fish Number..', fishnum # Block findblock = onlyfiles[ii].find('Block') findunderscore = [i for i in underscores if i > findblock][0] blocknum = onlyfiles[ii][findblock:findunderscore] print 'Block Number..', blocknum # Stimulus findstimulus = onlyfiles[ii].find('Blue') findunderscore = [i for i in underscores if i > findstimulus][0] stimulustype = onlyfiles[ii][findstimulus:findunderscore] print 'Stimulus type..', stimulustype # Region findregion = onlyfiles[ii].find('Hb') findunderscore = [i for i in underscores if i > findregion][0] region = onlyfiles[ii][findregion-1:findunderscore] print 'Region..', region targetDir = os.path.join(SaveFolder, fishnum, region) + filesep print 'Images will be saved in .....', targetDir if not os.path.exists(targetDir): os.makedirs(targetDir) cs = ChannelSeparator() cs.setId(path) bf = BFVirtualStack(path, cs, False, False, False) for sliceIndex in xrange(1,bf.getSize() + 1): print "Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) sliceFileName = os.path.join(targetDir, "T=" + str(sliceIndex) + ".tif") FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName)
def run(): # Chose a file to open od = OpenDialog("Chose File to Split", None) srcDir = od.getDirectory() if srcDir is None: # User canceled dialog return path = os.path.join(srcDir, od.getFileName()) # Chose a directory to store each slice as a file targetDir = DirectoryChooser("Chose folder in which to save").getDirectory() if targetDir is None: # User canceled the diolog return # Ready: cs = ChannelSeparator() cs.setId(path) bf = BFVirtualStack(path, cs, False, False, False) for sliceIndex in xrange(1,bf.getSize() +1): print "Well done. Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) sliceFileName = os.path.join(targetDir, str(sliceIndex) + ".tif") FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName)
def run(): # Choose a file to open od = OpenDialog("Choose multi-image file", None) srcDir = od.getDirectory() if srcDir is None: # User canceled the dialog return path = os.path.join(srcDir, od.getFileName()) # Choose a directory to store each slice as a file targetDir = DirectoryChooser("Choose target directory").getDirectory() if targetDir is None: # User canceled the dialog return # Ready: cs = ChannelSeparator() cs.setId(path) print "cs", cs bf = BFVirtualStack(path, cs, False, False, False) for sliceIndex in xrange(1, bf.getSize() + 1): print "Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) sliceFileName = os.path.join(targetDir, str(sliceIndex) + ".tif") FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName)
# Approach 1: using the "Raw" command with macro parameters for its dialog IJ.run( "Raw...", "open=%s image=%i-bit width=%i height=%i number=%i offset=%i big-endian" % (filepath, bitDepth, width, height, num_slices, headerSize + slice_offset)) # Approach 2: using LOCI bio-formats from loci.plugins.util import BFVirtualStack from loci.formats import ChannelSeparator from ij import ImagePlus, ImageStack try: cs = ChannelSeparator() cs.setId(filepath) bfvs = BFVirtualStack(filepath, cs, False, False, False) stack = ImageStack(width, height) for index in xrange(slice_index, slice_index + num_slices): stack.addSlice(bfvs.getProcessor(index)) title = os.path.split(filepath)[1] + " from slice %i" % slice_index imp = ImagePlus(title, stack) imp.show() finally: cs.close() # Approach 3: using low-level ImageJ libraries from ij.io import FileInfo, FileOpener fi = FileInfo() fi.width = width fi.height = height
if targetDir is None: # User canceled the diolog return if srcDir is None: # User canceled dialog return try: os.mkdir(targetDir) except Exception, e: print "Couldn't make directory:",e for f in os.listdir(srcDir): path = os.path.join(srcDir, f) # Ready: cs = ChannelSeparator() cs.setId(path) bf = BFVirtualStack(path, cs, False, False, False) fname = "%06d"%(index-index%200) path = os.path.join(targetDir,fname) try: os.mkdir(path) except Exception, e: print "Couldn't make directory:",e for sliceIndex in xrange(1,bf.getSize() +1): print "Well done. Processing slice", sliceIndex ip = bf.getProcessor(sliceIndex) string = "%06d.tif"%index sliceFileName = os.path.join(path, string) FileSaver(ImagePlus(str(sliceIndex), ip)).saveAsTiff(sliceFileName) index +=1 print "Finished:",path run(sys.argv[1])
import os from loci.plugins.util import BFVirtualStack, ImageProcessorReader print '---------------------------------------------------------------------------------------' base = '/scratch/imageproc/data/paolo/FluoView_stitching/oib_format/TVA4vglut2_n2_4X_20140126_234209' infile = 'Slide1sec001/Slide1sec001_01.oib' path = os.path.join(base, infile) #print 'file: %s' % path irdr = ImageProcessorReader() irdr.setId(path) bf = BFVirtualStack(path, irdr, False, False, False) print 'Successfully created virtual stack.' width = bf.getWidth() height = bf.getHeight() size = bf.getSize() procreader = bf.getReader() # meta = procreader.getMetadata() # print meta absposx = procreader.getMetadataValue('AbsPositionValueX') absposy = procreader.getMetadataValue('AbsPositionValueY') print 'Dimensions (x, y, size): %s, %s, %s' % (width, height, size) print 'Absolute positions: %s, %s' % (absposx, absposy)