Maintaining folder structure in batch processing


#1

Hi all,

I feel there is an easy solution to this, but my general unfamiliarity with the base code makes this a little finnicky to wiggle through. Based on this helpful thread (Sharing the generalization of 'Batch Processing' for multiple folders), I came up with the following code that will 1.) Move through an input folder of subfolders, each representing one dataset; 2.) Open each subfolder, which has 100-500 images; 3.) Convert all of those from DCM to 8-bit TIFF. What I would like is an additional 4.) Save all the converted files into subfolders with the same directory structure as the input folder. As it is now, it just saves them all in a huge mess within the output folder (though it does preserve the file names). Also, is there a way to 5.) Not open each image as a separate window so the Desktop is populated with thousands of windows? Or close them after each process loop?

My code:

// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".DCM") suffix

/*
 * Macro template to process multiple images in a folder
 */

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	open(input + File.separator + file);
	run("8-bit");
	saveAs("TIFF", output + File.separator + file);
}

#2

Dear @enteril,

What I would do for this, is to get the folder names of the input and output directories (master folders) and then when it comes to saving, find the path of the file and replace the input folder string with the output folder string and create that directory. That way you will end up with the same folder tree for the output as the input. It will be something like this:

// @File(label = "Input directory", style = "directory") input
// @File(label = "Output directory", style = "directory") output
// @String(label = "File suffix", value = ".DCM") suffix

/*
 * Macro template to process multiple images in a folder
 */

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

var outputDir // global variable to be passed to function later
var inputDir 
outputDir = File.getName(output); // retrieves the folder name string of the master input folder selected by the user above
inputDir = File.getName(input);

setBatchMode(true);
processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	open(input + File.separator + file);
	run("8-bit");
	saveDir = replace(input, inputDir, outputDir); // replaces the input folder name (string) with the output folder name (string)
	File.makeDirectory(saveDir); // makes the above directory
	saveAs("TIFF", saveDir + file);
}

For this you can use batch mode, I have included this in the script above. When batch mode is “true”, no windows will appear, you can see the other arguments by following the link.

Let me know if this works,

Rob


#3

This works like a charm. Thank you so much! It’s also very easily editable and flexible for additional processes!