Ejemplo n.º 1
0
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(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)
Ejemplo n.º 3
0
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