Fiji macro suddenly doesn't work


I have a macro working for once but not worked since then. It was a macro to open, process and save images in a folder one by one. Now, every time i ran the macro, nothing happened (no image open, no process, but also no error reminder). Does anyone have an idea about what is going on there? Any clues are highly appreciated!!!

1 Like

Hi @Tian_Tian

would you mind sharing the macro so that we can take a look?


1 Like
dir = getDirectory("Choose a Directory"); 

for (i=0;i<nImages;i++) { 
        title = getTitle; 
run("Duplicate...", " ");
run("Enhance Local Contrast (CLAHE)", "blocksize=127 histogram=256 maximum=3 mask=*None* fast_(less_accurate)");
run("Find Edges");
setOption("BlackBackground", true);
run("Make Binary");

imageCalculator("Divide create 32-bit", original,original);

imageCalculator("Multiply create 32-bit", "Divided.tif","Inverted.tif");

setOption("ScaleConversions", true);
results_Dir=("E:/New folder (3)");
        saveAs("tiff", results_Dir+title); 
        run("Close All");

Thank you for asking. I posted the macro below.

Hi @Tian_Tian,

your macro seems to be missing the part where it opens images altogether. It (kinda) works if you run it after images are already open in Fiji, but otherwise you just have nImages equals to 0 at the beginning and never enter the for loop!
An option is to use the Process Folder template macro and introduce your processing steps on the processFile function:

 * Macro template to process multiple images in a folder

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

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


// 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);
	print("Saving to: " + output);

1 Like

Many thanks @erickratamero !!
In the script you posted, is the image selected named as list[i]? Or I have to say something like “original=list[i]” or “original=getImageID” then processing the following steps (duplicate, rename, select original image etc.)?

You don’t need to touch the processFolder part of the macro; focus on processFile. Here, the path to each individual image will be on the input and file variables, so you can put a line like

open(input + File.separator + file);

between the two print statements; that will open each individual image. From there, you probably want title = getTitle; and everything from original=getImageID; onwards.

Thank you for your help! Yes it worked well, except that the image was not saved into the output. I put all processing scripts in processFile part (without “results_Dir=” and “saveAs()”). After the image processing was done, the processed image was not saved in the output. Did I understand correctly? Or did I do something wrong?

You need a saveAs command somewhere (probably something like saveAs("tiff", output+title); towards the end), otherwise Fiji has no way of knowing you want to save anything!

Thank you for your help! I found my problem, and it all works now!

I really appreciate it.

1 Like