Sript to select image in imageJ

Hi All,

I am new in imagej world. Suppose we have a list of stack images (contain 862 images or slices each stack) and I need to select the first ant the laste image of each stack then put them in a individual stack ( a stack who contain the first image and a stack who contain the last image ) . i try to do 2 script one for the first image where i used setSlice (1) to select the first image and another script to select the last image but unfortunately the 2 didnt 'worl thanks for your help my script to select firt image was :

j = 1

m = 0

//Select folder containing stacks to process.
input = getDirectory("Choose substack Directory");


//Create output folder.
File.makeDirectory(input+"\Concatenate");
output = input+"\selection"

Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();


// DETERMINE NUMBER OF .TIF FILES ("m")
list = getFileList(input);
   for (i=0; i<list.length; i++)
	if (endsWith(list[i], suffix)) 
		m = m+1;

setBatchMode(true);  
list = getFileList(input);
   for (i=0; i<list.length; i++)
	setSlice(1) {
		processFile(input, output, list[i]);
		j = j+1;
		showProgress(j);
		
}

setBatchMode(false);

function processFile(input, output, file) {
	open(list[i]);
	run("selection",+ " image1=" +list[i] );
	saveAs("tiff", input+"\\selection \\"+ list[i]);
	close();
	}
print(j+1, " selection : " + input+"\selection");

Welcome to the forum, @louna25!

(I edited your post to make the code more readable.)

Did you look at the error message you get when trying to run your script? I’m sure it would have helped you to spot the error:

There’s a syntax error here: the for loop has to start with a curly brace ({) and only then can follow any command like setSlice(1). Have a look at the macro language documentation.

Hope that helps.

3 Likes

thank you for your answer. about the syntax error , the form here contain some mistake but the original one no:

m = 0

//Select folder containing stacks to process.
input = getDirectory("Choose substack Directory");


//Create output folder.
File.makeDirectory(input+"\Concatenate");
output = input+"\Concatenate"

Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();


// DETERMINE NUMBER OF .TIF FILES ("m")
list = getFileList(input);
   for (i=0; i<list.length; i++)
    if (endsWith(list[i], suffix)) 
        m = m+1;

setBatchMode(true);  
list = getFileList(input);
   for (i=0; i<list.length; i=i + 1) {
     setSlice(1);
                    processFile(input, output, list[i]);
}

setBatchMode(false);

function processFile(input, output, file) {

    
    run("Selection...", "  title=" +list[i] );
    saveAs("tiff", input+"\\Selection\\"+ list[i]);
    close();

the error message is always:

this command Requires a selection

I try another script but in this case i have a different message error

';'expected in line 38
<run> (''Make Substack...", "Slices="+ slices) ;

the other script :

m = 0

//Select folder containing stacks to process.
input = getDirectory("Choose substack Directory");
//Create output folder.
File.makeDirectory(input+"\Concatenate");
output = input+"\Concatenate"

Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();


// DETERMINE NUMBER OF .TIF FILES ("m")
list = getFileList(input);
   for (i=0; i<list.length; i++)
    if (endsWith(list[i], suffix)) 
        m = m+1;
title=getTitle();
selectWindow(title);
run("Make Substack...", "  slices=1");

title=getTitle();
s = 1;
while (s <= nSlices) {
  selectWindow(title);
  slices="+s+";
 run("Make Substack...", "  slices=" + slices);
  save(title + "-" + slices + ".tif");
  close();
  s ++;
}

Indeed, the command run("Selection...") calls File > Save As > Selection… and requires an active selection in the current image, which you don’t have when running your macro.

How did you get that command anyways? I didn’t find a command in the menu where the Macro recorder records this title= option string.
Also, you shouldn’t use list[i] in that function, as it is not defined in the scope of the function. Its input parameters are input, output, and file; so the value of list[i] that you used when calling the function is now in your file variable.


In the future, please make sure to copy and paste your code exactly as you’re running it (and check your post for errors before hitting the Reply button). Otherwise trying to help you gets a bit frustrating.

2 Likes

hi
Thanks for your as usual quick feed back!
About this command “Selection…”, " title=" +list[i] , i have a script sample to concatenate 2 images where they use a similar command ( concatenate in place of selection ) anyway i will try to modify the scipt to select the differents images
thanks