Create a new folder with the name of each slice

imagej
macro

#1

Hi everyone,

I have 3 images grouped in a stack. I look for the way to create a new folder for each slice in order to save the data of slice 1 in the folder 1.

I began with this script but I don’t know how to selection the name of the slice and include it in the nomination of the folder…

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

list = getFileList (input);
for (i=0; i<list.length; i++) {
	incoming= input+list[i];
}
run("Image Sequence...", "open= [" + incoming + "] convert");

n=nSlices;
for (slice=1; slice<=n; slice++) {
	fileName= setSelectionName(slice);
	File.makeDirectory (output + File.separator + fileName);
}

Can anyone help me ?
Thank you :slight_smile:


#2

I believe the below should work for you.

    n=nSlices;
    for (slice=1; slice<=n; slice++) {
    	setSlice(slice);                                           
    	sliceName = getInfo("slice.label");        
    	File.makeDirectory(output + File.separator + sliceName + File.separator) 
    }

Hope that helps you!


#3

Thanks for your quick answer ! That is exactly that what I looked :slight_smile:


#4

Finally, I don’t find the way to save each slice in their own file… They always take place in the same folder (the last). I modified my code like this

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

list = getFileList (input);
for (i=0; i<list.length; i++) {
	incoming= input+list[i];
}
run("Image Sequence...", "open= [" + incoming + "] convert");
run("Set Scale...", "distance=0.4700 known=1 pixel=1 unit=µm global");
makeRectangle(0, 232, 1600, 706);
run("Duplicate...", "duplicate");
run("Sharpen", "stack");

setAutoThreshold("Intermodes");
//run("Threshold...");
setOption("BlackBackground", false);
run("Convert to Mask", "method=Intermodes background=Light calculate");

n=nSlices;
for (slice=1; slice<=n; slice++) {
	setSlice(slice);                                           
    sliceName = getInfo("slice.label");
    myDir = output + File.separator + sliceName + File.separator ;    
    File.makeDirectory(myDir);
}
    run("Image Sequence... ", "format=JPEG digits=10 use save=myDir");

At first, I would like to create an image sequence in order to apply the same threshold on each picture. Then, my objective is to analyze every image and obtain the particle analysis results for each of them and save data of slice 1 in the folder 1 for example

Do you have advice to give me or documents which could be useful for me?
Thank you :slight_smile:


#5

Hi Efiji,

Here are some topics you could look into regarding writing macros.

I’ll go through each section and try and comment what I think about it to help.

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

The only thing I would say about the above is your third line seems to be superfluous since you never use the variable suffix anywhere else so it can be removed.

list = getFileList (input);
    for (i=0; i<list.length; i++) {
    incoming= input+list[i];
    }

This looks fine although you end the for loop surprisingly early. If you are trying to process multiple images using this macro it won’t work so it will loop through the list of filenames and change what incoming is defined as but nothing will happen to the different images. You need to put the closing bracket after everything has been done to each image before incoming is changed.

run("Image Sequence…", "open= [" + incoming + "] convert");
run("Set Scale…", "distance=0.4700 known=1 pixel=1 unit=µm global");
makeRectangle(0, 232, 1600, 706);
run("Duplicate…", "duplicate");
run("Sharpen", "stack");

This seems mostly fine although for some reason the run("Image Sequence…) line isn’t working for me but if it’s working for you then you should obviously leave it.

setAutoThreshold("Intermodes");
//run("Threshold…");
setOption("BlackBackground", false);
run("Convert to Mask", "method=Intermodes background=Light calculate");

This looks fine I think although you don’t really need to concert to mask to use Analyze Particles. Have you been using the macro recorder to create these commands as you will want to use that to set your Analyze Particles parameters in this next section.

n=nSlices;
for (slice=1; slice<=n; slice++) {
  setSlice(slice);
  sliceName = getInfo(“slice.label”);
  myDir = output + File.separator + sliceName + File.separator ;
  File.makeDirectory(myDir);
}

The only thing is really you need to generate and save the Analyze Particles output in here. Something like.

run("Analyze Particles...", "size=10.00-Infinity display slice");

And finally

run("Image Sequence… ", "format=JPEG digits=10 use save=myDir");

I don’t know what the above line is doing. I assume you want to save some sort of image? Is it the cropped rectangle stack, or each slice?

Additionally, I really wouldn’t use JPEGs if I were you. See here: why you shouldn’t be using JPEGs for image analysis.

I’ve put the below script where I’ve tried to put into practice what I’ve said. It currently only save the Results tables and not any images. Also, as I said, the run("image Sequence…) command wasn’t working for me so I’ve replaced it with something more simple for me to run something that works for me but if you need to import an imge sequence you should put it in. So basically you should see this is as a starting point.

#@File (label = "Input directory", style = "directory") input
#@File (label = "Output directory", style = "directory") output

list = getFileList (input);

for (i=0; i<list.length; i++) {
	incoming = input + File.separator + list[i];
	
	open(incoming);
	
	run("Set Scale...", "distance=0.4700 known=1 pixel=1 unit=µm global");
	makeRectangle(0, 232, 1600, 706);
	run("Duplicate...", "duplicate");
	run("Sharpen", "stack");
	
	setAutoThreshold("Intermodes");
	
	for (slice=1; slice<=nSlices; slice++) {
		setSlice(slice);
		sliceName = getInfo("slice.label");
		myDir = output + File.separator + sliceName + File.separator ;
		File.makeDirectory(myDir);
		run("Analyze Particles...", "size=0-Infinity display slice");
		selectWindow("Results");
		saveAs("Results", myDir + sliceName + "_Results.csv");
		run("Close");
	}
}

Hope that helps!


#6

Thanks Imurphy ! I’ll take into account your remarks and apply them to my code
This script does not work for my case but I found an alternative