Make substack from hyperstack

I’ve got a hyperstack that contains 15 individual images, each with 5 channels and 21 slices. The images are sequential in the stack, so I am trying to make substacks from slices 1-21, 22-43… etc. I’ve written a macro that runs stack splitter and splits into the 15 stacks, but when it is naming and saving the individual images, for some reason it is only saving every second one (currently even numbers). I think this has to with the counter, because when I set the counter to 2 it saves the odds… either way I’m sure it’s a simple fix but appreciate any input.


dir1 = getDirectory("Choose Source Directory");
dir2 = getDirectory("_Choose Destination Directory");
list = getFileList(dir1);
//open and split stacks
for (i=0; i<list.length; i++){
	run("Stack Splitter" , "number=15");
//save all as new images
for (i=1; i<nImages; i++) { 
	saveAs("tif", dir2+title); 


Where do you define nImages? I’m assuming it’s just 15 in this case…

I think the issue is that you are closing the images as you go… so your number of images is shrinking as you are incrementing… therefore skipping images to save. That’s why it’s every-other…

Use print() statements as you move through your loops - print out which image you are working on in each loop so you can follow along and find the error.

The above is just a guess though - since I could not run your code on anything. If you provide an example image dataset and a complete code snippet to highlight this issue - we can better assist in fixing it directly.

Thank you! The close was indeed the problem. I appreciate your help.

1 Like