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