Problem saving images inside a loop

Hi!.
I would like to create a macro for my images and I used a loop to process all images inside of a folder. But, when I tried to save the images that I processed, the macro only saves the first but the no the others.
Someone who could help me?. I am new on this by the way.
Thanks!

//Loop
for (i = 0; i < 3; i++) {
open(img_path);
title = getTitle();

//duplicate only one image of the stack
run(“Duplicate…”, " ");
saveAs(“tiff”, output_path);

}

Hi @Zahady,

Welcome to the community. We are here to help. the more info you give, the better we can help.
Likely, the error is in the part of your code that you left out. Please post the entire code, and the (first few itemsof the) folder listing. Especially the output_path construction is of importance if you want help.

Hi!
Thanks for asking me. Here zou have all macro.

//close all the images to start fresh
run(“Close All”);

//Get the path to the input images
#@File(label = “Imput directory”, style = “directory”) img_dir

//define input Dir
img_dir +=File.separator;

//get a list fo all files in the input folder
list = getFileList(img_dir);

//For a loop
for (i = 0; i < list.length; i++) {
if (endsWith(list[i], “.tif”)) {

//open the tif fyle
open(img_dir+list[i]);

//make projection
run("Z Project...", "stop=7 projection=[Sum Slices]");

//saving in the output folder
outputName = substring(list[i], 0 , lengthOf(list[i])-4) + "_output";

output_path = output_dir+outputName;
saveAs("tif", output_path);
run("Close All");

}

}

Hi @Zahady,

Did you copy-paste the code from your macro? The quotes are of three different types, suggesting your manually typed the code in the forum editor.

When pasting in the forum edit pane, enclose the code using the </> tool in the edit window. This keeps the code copy-pastable, colour codes the commands and preservers indents.

Actually, I wonder why your macro would work at all, as I expect an “Undefined identifier in line…” output_path = <output_dir>+outputName; message - I can’t find where you defined the output_dir variable. Or am I overlooking something?

Oops... output_dir?
//close all the images to start fresh
run("Close All");

//Get the path to the input images
#@File(label = "Imput directory", style = "directory") img_dir

//define input Dir
img_dir +=File.separator;

//get a list fo all files in the input folder
list = getFileList(img_dir);

//For a loop
for (i = 0; i < list.length; i++) {
	if (endsWith(list[i], ".tif")) {

		//open the tif fyle
		open(img_dir+list[i]);

		//make projection
		run("Z Project...", "stop=7 projection=[Sum Slices]");

		//saving in the output folder
		outputName = substring(list[i], 0 , lengthOf(list[i])-4) + "_output";

		output_path = output_dir+outputName;
		saveAs("tif", output_path);
		run("Close All");
	}
}

I pasted the macro directly. However, here it is

I did that you suggested me and now nothing happens jiji

Hi @Zahady,
Sorry, I’m having trouble understanding you (“now nothing happens jiji”). In your screen shot, I see that you define the output dir in line 18, so that is OK. Apparently you did not copy the macro and paste it into the Forum, as the two texts differ. Anyhow. A screen shot text cannot be copied. So please, next time use the copy-paste into the forum and use the </> button in the edit window to get your text recognised as code, rather than posting a screen shot.

I see you are using FIJI. For simple tasks like this, debugging in ImageJ1 is easier than in ImageJ2 (FIJI). Once your code works OK, you can move to FIJI as FIJI understands ImageJ1, but not the other way around.

I suggest we try to solve your issue using an example stack and one stack only, (not the whole directory) combined with a tmp folder (this we can all generate independent of or operating system of local data) to get your code going.
Once we get the sample going, you can do the loop over the file names in your won environment. Currently the NIH site is down, as soon as I find it back, I’ll post some code.