// Written by Kevin Fisher #@ File (label = "Input directory", style = "directory") srcDir #@ File (label = "Output directory", style = "directory") dstDir #@ String (label = "Input file extension", value=".tif") ext #@ String (label = "Output file format", choices={"Tiff", "Jpeg", "Bmp", "Png"}, style="listBox") fileFormat #@ Boolean (label = "Flatten (convert to RGB)", value = true) flatten #@ Boolean (label = "Use Bio-Formats to open files", value = false) useBF #@ Boolean (label = "Include subdirectories", value = true) recurse #@ Boolean (label = "Keep directory structure", value = true) keepDirectories #@ String (visibility = MESSAGE, label = "", required = false) separator #@ String (label = "Channel 1 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch1Color #@ String (label = "Channel 2 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch2Color #@ String (label = "Channel 3 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch3Color #@ String (label = "Channel 4 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch4Color #@ String (label = "Channel 5 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch5Color #@ String (label = "Channel 6 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch6Color #@ String (label = "Channel 7 Color", choices = {"None", "Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays"}) ch7Color import ij.IJ import loci.plugins.BF import ij.CompositeImage channels = [ch1Color, ch2Color, ch3Color, ch4Color, ch5Color, ch6Color, ch7Color] def main() { // load every file in directory (either recursive or not) if (recurse) { srcDir.eachFileRecurse { name = it.getName() if (name.endsWith(ext)) { process(it) } } } else { srcDir.eachFile { name = it.getName() if (name.endsWith(ext)) { process(it) } } } } def process(file) { println "Processing $file" // load image def imp = useBF ? BF.openImagePlus(file.getAbsolutePath())[0] : IJ.openImage(file.getAbsolutePath()) // merge channels into a composite imp = new CompositeImage(imp, CompositeImage.COMPOSITE) def actChannels = imp.getActiveChannels() // loop through all channels and assign colors 1.upto(imp.getNChannels()) { imp.setC(it) if (channels[it - 1] == "None") { actChannels[it - 1] = false; } else { actChannels[it - 1] = true; IJ.run(imp, channels[it - 1], "") } } // convert from Boolean[] to String imp.setActiveChannels(new String((char[]) channels.collect { it ? '1' : '0' })) // flatten to single image if required if (flatten) { imp = imp.flatten(); } // if rebuilding directory structure, figure out the current subdirectory def subDir = keepDirectories ? srcDir.toPath().relativize(file.getParentFile().toPath()).toString() : "" // no relative path // calculate save directory from destination folder and subdirectory def saveDir = new File(dstDir.toPath().toString(), subDir) if (!saveDir.exists()) saveDir.mkdirs() // save image as whatever format def saveFile = new File(saveDir, file.getName()) IJ.saveAs(imp, fileFormat, saveFile.getAbsolutePath()) // Clean up imp.close() } main()