/* Process_Folder_IJM.ijm * IJ BAR snippet https://github.com/tferr/Scripts/tree/master/Snippets * * This macro implements a a reusable batch processor[1] that processes a folder * of files of a certain type. It is a more flexible alternative to the IJ1 * Process>Batch>Macro built-in command. * * It is composed of four functions: * 1. getOutputDirectory(): Sets a destination folder to save processed images * 2. validExtension(): Determines which file extension(s) should be processed * 3. processFiles(): Applies myRoutines() to each individual image * (Processed images are save as TIFF in output directory) * 4. myRoutines(): Container to hold the image processing routines * (In this example, it randomizes the image name to allow blind analysis) * * Because all the tasks required to iterate through the files are handled by * separated functions, only the myRoutines() function needs to be edited, e.g., * with code generated by the Macro Recorder. It can also call macros and scripts * saved elsewhere (see http://fiji.sc/BAR#ProcessingFunction) * * Resources * [1] http://fiji.sc/BAR#Batch_Processors * https://github.com/tferr/Scripts/tree/master/Snippets#batch-processors * https://github.com/tferr/Scripts/tree/master/Snippets#imagej-macro-language * http://rsb.info.nih.gov/ij/macros/BatchProcessFolders.txt */ // Define input directory inputDir = getDirectory("Select a source directory"); // Create output directory outputDir = getOutputDirectory(inputDir); // Iterate through inputDir (ignoring files with non-specified // extensions) and save a copy of processed images in outputDir processFiles(inputDir, outputDir); // Proudly inform that all the processing is done if (getBoolean("All done! Reveal output directory?")) call("bar.Utils.revealFile", outputDir); /* * This function defines the image manipulation routines. In this example, it * renames the active image using a random string. Note that we do not need to * worry about opening, closing and saving the image as .tif without overriding * the original file. Those operations are already performed by . */ function myRoutines() { // We'll first define a new function that generates a random // string. See http://fiji.sc/BAR#IJ_Macro_Language for details function randomString(length, spacers) { template = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; nChars = lengthOf(template); string = ""; for (i=0; i. For safety, the macro is aborted if * is not accessible or if the directory cannot be created, e.g., due to lack of * appropriate credentials. It does nothing if such a directory already exists. */ function getOutputDirectory(input_dir) { // Check if path to input_dir is valid if (!File.isDirectory(input_dir)) { exit("Macro aborted: The directory\n'"+ input_dir +"'\nwas not found."); } // Ensure the string defining input_dir does not end in '/' or '\' if (endsWith(input_dir, File.separator)) { separatorPosition = lengthOf(input_dir); input_dir = substring(input_dir, 0, separatorPosition-1); } // Create output directory (unless it already exists) output_dir = input_dir + "_Processed" + File.separator; if (!File.isDirectory(output_dir)) { File.makeDirectory(output_dir); if (!File.isDirectory(output_dir)) exit("Macro aborted:\n" + output_dir + "\ncould not be created."); } return output_dir; } /* * This function returns true if the file extension of the argument * is present in the array. Returns false otherwise. */ function validExtension(filename) { extensions = newArray(".tif", ".stk", ".oib"); valid = false; for (i=0; i to individual files filtered by * in and all of its subdirectories. The * argument specifies the directory where a a TIFF copy of * each processed image will be saved. It does nothing if * does not exist. */ function processFiles(input_dir, output_dir) { // Do not display images during macro execution setBatchMode(true); // Get contents of input directory (files and subdirectories) files = getFileList(input_dir); // Loop through the file list. We will check if input_dir/output_dir // exist since that task was already performed by getOutputDirectory() for (i=0; i