示例#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)
示例#3
0
def dimensionsOf(path):
    fr = None
    try:
        fr = ChannelSeparator()
        fr.setGroupFiles(False)
        fr.setId(path)
        return fr.getSizeX(), fr.getSizeY()
    except:
        print sys.exc_info()
    finally:
        fr.close()
def bit_tester(image_name):
    ## gets the number of bits for an image, error if not 8 or 16 bit. ##
    dest = IJ.getDirectory("image")
    filepath = dest + image_name
    fr = ChannelSeparator()
    fr.setId(filepath)
    bitDepth = fr.getBitsPerPixel()
    if int(bitDepth) in (8, 16):
        if int(bitDepth) == 8:
            bit_depth = 2**(bitDepth)-1
        if int(bitDepth) == 16:
            bit_depth = 2**(bitDepth)-1
        return bit_depth
    elif int(bitDepth) not in (8, 16):
        print("Error image is not 8 or 16 bit")
        quit()
示例#5
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)
from loci.formats import ChannelSeparator
import os, sys

# Read the dimensions of the image at path by parsing the file header only,
# thanks to the LOCI Bioformats library

filepath = "/home/albert/Desktop/t2/bat-cochlea-volume.tif"

try:
    fr = ChannelSeparator()
    fr.setGroupFiles(False)
    fr.setId(filepath)
    width, height, nSlices = fr.getSizeX(), fr.getSizeY(), fr.getSizeZ()
    n_pixels = width * height * nSlices
    bitDepth = fr.getBitsPerPixel()
    fileSize = os.path.getsize(filepath)
    headerSize = fileSize - (n_pixels * bitDepth / 8)  # 8 bits in 1 byte
    print "Dimensions:", width, height, nSlices
    print "Bit depth: %i-bit" % bitDepth
    print "Likely header size, in number of bytes:", headerSize
except:
    # Print the error, if any
    print sys.exc_info()
finally:
    fr.close()  # close the file handle safely and always

# For parsing TIFF file headers
from java.io import RandomAccessFile
from java.math import BigInteger