Make z-stack images with scripts

Hello,
I have lots of images from which I need to make Z-stack images (each stack has to be made out of 5 separate image). how to make it automatically?
thanks a lot!

You can import an image sequence as a stack, see:

https://imagej.nih.gov/ij/docs/guide/146-26.html#toc-Subsection-26.6

From an exisitng stack you can create substacks, etc. (see Tools section):

https://imagej.nih.gov/ij/docs/guide/146-28.html#toc-Subsection-28.6

All commands can be recorded if you want to excecute the actions programmatically, see:

https://imagej.nih.gov/ij/docs/guide/146-31.html#sub:Record…

1 Like

Hello,
Thanks for your reply!
have folder with 10000 images and with Imagej need to do it separated which is really time-consuming. Thought if there is any script which I can use to make 5 image stacks automatically

Did you already try to import using image sequence and then convert Image>Hyperstacks>Stack to Hyperstack…?

Hi @Niasiradze,

Do they have a specific naming pattern? In other words, how do you know how to group them and in which order?

Perhaps you can post a short example filename list to get a general idea.

Nico

hey! @NicoDF
Images were taken with scanning microscope where you can set up amount of z-stacks you want to have, so in my case I have set 5 stacks per image. about naming it’s regular starts with Tile000001 and goes up…

@eljonco Tried, but problem is I don’t have stacks so cant do it…

First you import all files as sequence of images, that gives you a stack with interleaved images. Using Image>Hyperstack>Stack to hyperstack you can de-interleave and re-organise.

@eljonco When I am trying to import folder (with images) doesn’t gives me chance to import all images

@Niasiradze
How is that? Do you get an error message in the Sequence Reader window that pops up if you do File>Import>Image Sequence…? If not: what doesn’t give?

Alternatively, you can import sequences of certain kind, (eg. the red channel images), make stacks and then mess with stack reshuffling.

@eljonco cannot select, only chance is to select 1 image…

Hi @Niasiradze,

What @eljonco is referring to is the following:

  1. Go to the menu item: File > Import > Image Sequence
    menu

You’ll be presented with a folder selection dialog. Choose the directory where all the images reside (the one that you listed in the image, in your case);

  1. In the following dialog, select the import parameters:

You can open a single stack like this (which opens a 5-image stack starting from image 21, using only images that contain “Tile” in the name):

imagen

You can also open all the images as a long stack (the selected number should be the total number of relevant images in the folder, and also a multiple of 5, which is not my case):

imagen

And then run the command Image > Stack > Tools > Stack Splitter, and enter the number of stacks (number of files / 5).
imagen

Be aware that in your case, you might run into memory problems if you have 1000 images of 4.2 Mb each.

Now a question: is your goal to open all stacks at once, one at a time, o save the image collection as stacks for further processing?

@NicoDF my goal is to have 5 - image stacks for future processing.

Ok, @Niasiradze,

This macro will do the trick then:

#@ File (label = "Input directory", style = "directory") input_directory
#@ File (label = "Output directory", style = "directory") output_directory
#@ String (label = "File suffix", value = ".tif") suffix
#@ String (label = "File pattern", value = "Tile") pattern
#@ Integer (label = "Stack size", value = 5) stack_size


filelist = getFileList(input_directory);
Array.sort(filelist);
filteredList = newArray();

for (i = 0; i < filelist.length; i++) {
	if(indexOf(filelist[i], pattern)>-1 && endsWith(filelist[i], suffix))
		filteredList = Array.concat(filteredList, filelist[i]);
	}

setBatchMode(true);
for (i = 0; i < filteredList.length; i+=stack_size) {
	run("Image Sequence...", "open=&input_directory number=&stack_size starting=&i file=&pattern sort");
	stackname =  "stack -" + filteredList[i] + ".tif";
	save(output_directory + File.separator + stackname);
	print("Saved:", stackname);
	close();
	}	
setBatchMode(false);

print("Done!");

Just specify the input and output folders, the file extension (I guess tiff, but the screenshot does not really show), some pattern to only include the files you’re interested in, and the stack size (5 in your case):

imagen

Let me know if it works for you.

Cheers,
Nico

tried to run, but doesn’t do anything.
you can see it here



  1. file types are .bmp
  2. before mentioned way works but I have to save all stacks manually.
    cheers,

That’s strange. Let’s try with this slightly modified version (some extra verbose output added) to see if we can understand the issue:

#@ File (label = "Input directory", style = "directory") input_directory
#@ File (label = "Output directory", style = "directory") output_directory
#@ String (label = "File extension", value = ".bmp") suffix
#@ String (label = "File pattern", value = "Tile") pattern
#@ Integer (label = "Stack size", value = 5) stack_size


print("Input directory", input_directory);
print("Output directory", output_directory);

filelist = getFileList(input_directory);
Array.sort(filelist);
print(filelist.length, "files in input folder");
filteredList = newArray();

for (i = 0; i < filelist.length; i++) {
	if(indexOf(filelist[i], pattern)>-1 && endsWith(filelist[i], suffix))
		filteredList = Array.concat(filteredList, filelist[i]);
	}
print(filteredList.length, "files filtered for making stacks"); 

setBatchMode(true);
for (i = 0; i < filteredList.length; i+=stack_size) {
	run("Image Sequence...", "open=&input_directory number=&stack_size starting=&i file=&pattern sort");
	stackname =  "stack -" + substring(filteredList[i], 0, lengthOf(filteredList[i])-lengthOf(suffix)) + ".tif";
	save(output_directory + File.separator + stackname);
	print("Saved:", stackname);
	close();
	}	
setBatchMode(false);

print("Done!");

You can just copy this code, open a new tab in the script editor (ctrl + shift + n) and paste it. There’s no need to use the recorder. Beware that both the file extension as well as the file pattern are case sentitive, so “.BMP” is not the same as “.bmp”. Make sure you match those to your files.

Let me know how it goes.
Nico