imcflibs.imagej.split

Functions for splitting channels and or slices.

 1"""Functions for splitting channels and or slices."""
 2
 3import os
 4
 5from ij import IJ, ImagePlus  # pylint: disable-msg=E0401
 6from ij.io import FileSaver  # pylint: disable-msg=E0401
 7from ij.plugin import ChannelSplitter  # pylint: disable-msg=E0401
 8
 9
10def split_by_c_and_z(log, dname, imgf, skip_top, skip_bottom):
11    """Helper function to open, split and save a file.
12
13    Load the file specified, split by channels and z-slices, create a directory
14    for each channel using the channel number as a name suffix and export
15    each slice as an individual TIF file.
16
17    Parameters
18    ----------
19    log : logger or scijava-logservice
20        The logger object to be used for logging.
21    dname : str
22        The directory to load TIF files from.
23    imgf : str
24        The file name to load and split.
25    skip_top : int
26        Number of slices to skip at the top.
27    skip_bottom : int
28        Number of slices to skip at the bottom.
29    """
30    log.info("Processing file [%s]" % imgf)
31    imp = IJ.openImage(dname + "/" + imgf)
32    fname = os.path.splitext(imgf)
33    channels = ChannelSplitter().split(imp)
34    for channel in channels:
35        c_name = channel.getTitle().split("-")[0]
36        tgt_dir = os.path.join(dname, fname[0] + "-" + c_name)
37        if not os.path.isdir(tgt_dir):
38            os.mkdir(tgt_dir)
39        stack = channel.getStack()
40        for z in range(1 + skip_top, stack.getSize() + 1 - skip_bottom):
41            proc = stack.getProcessor(z)
42            fout = "%s/%s-z%s%s" % (tgt_dir, fname[0], z, fname[1])
43            # fout = dname + "/" + c_name + "/" + fname[0] + "-z" + z + fname[1]
44            log.info("Writing channel %s, slice %s: %s" % (c_name, z, fout))
45            FileSaver(ImagePlus(fname[0], proc)).saveAsTiff(fout)
def split_by_c_and_z(log, dname, imgf, skip_top, skip_bottom):
11def split_by_c_and_z(log, dname, imgf, skip_top, skip_bottom):
12    """Helper function to open, split and save a file.
13
14    Load the file specified, split by channels and z-slices, create a directory
15    for each channel using the channel number as a name suffix and export
16    each slice as an individual TIF file.
17
18    Parameters
19    ----------
20    log : logger or scijava-logservice
21        The logger object to be used for logging.
22    dname : str
23        The directory to load TIF files from.
24    imgf : str
25        The file name to load and split.
26    skip_top : int
27        Number of slices to skip at the top.
28    skip_bottom : int
29        Number of slices to skip at the bottom.
30    """
31    log.info("Processing file [%s]" % imgf)
32    imp = IJ.openImage(dname + "/" + imgf)
33    fname = os.path.splitext(imgf)
34    channels = ChannelSplitter().split(imp)
35    for channel in channels:
36        c_name = channel.getTitle().split("-")[0]
37        tgt_dir = os.path.join(dname, fname[0] + "-" + c_name)
38        if not os.path.isdir(tgt_dir):
39            os.mkdir(tgt_dir)
40        stack = channel.getStack()
41        for z in range(1 + skip_top, stack.getSize() + 1 - skip_bottom):
42            proc = stack.getProcessor(z)
43            fout = "%s/%s-z%s%s" % (tgt_dir, fname[0], z, fname[1])
44            # fout = dname + "/" + c_name + "/" + fname[0] + "-z" + z + fname[1]
45            log.info("Writing channel %s, slice %s: %s" % (c_name, z, fout))
46            FileSaver(ImagePlus(fname[0], proc)).saveAsTiff(fout)

Helper function to open, split and save a file.

Load the file specified, split by channels and z-slices, create a directory for each channel using the channel number as a name suffix and export each slice as an individual TIF file.

Parameters
  • log (logger or scijava-logservice): The logger object to be used for logging.
  • dname (str): The directory to load TIF files from.
  • imgf (str): The file name to load and split.
  • skip_top (int): Number of slices to skip at the top.
  • skip_bottom (int): Number of slices to skip at the bottom.