Opening multiple images in macro

Sample image and/or code

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z001_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z002_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z003_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z004_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z005_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z006_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z007_Overlay.tif");

open("D:/SnapMirror/ncc18jq_data/Research/Stem_Cell_Neural_Network/IPSC_images/CS20200610-well3/merged0717/20200717-20times-5_00219_Z008_Overlay.tif");

run("Images to Stack", "name=0219_Overlay_Stack title=[] use");

run("Stack Focuser ", "enter=11");
close("0219_Overlay_Stack");
selectWindow("Focused_0219_Overlay_Stack");

// Color Thresholder 2.1.0/1.53c
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=126;
max[0]=129;
filter[0]="pass";
min[1]=1;
max[1]=223;
filter[1]="pass";
min[2]=230;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);
// Colour Thresholding-------------

setOption("BlackBackground", true);
run("Convert to Mask");
run("Watershed");

run("Analyze Particles...", "size=50-Infinity pixel show=Outlines display summarize add");

Background

I am attempting to create a macro that can open 8 z stack images at a time, but I am unsure on how to automate the opening of these images. In my macro above, you can see that I am opening specific images, but I want to be able to generalize this. The images are all in the same file and are only organized by their file names (20200717-20times-5_00219_Z008_Overlay.tif). “219” is the specific image number and I want to be able to open all 8 images that contain that same image number.

Analysis goals

  • What information are you interested in getting from this image? I am attempting to create a full focus image of immunostained neurons from 8 z stacks and then analyze the number of particles that fall within a certain color range.

Challenges

I’m not sure how to write the script to open 8 z stacked images simultaneously that have the same file name (the only part of the name that changes is Z001-Z008). I’m really new to this so I’m sorry if I’m not explaining my problem well!

Hi Natalie,

Welcome to the forum.

The short macro code below should do what you want. It defines a searchString at the start (you could use a dialog to do this, which would allow you to define this every time you run the macro). It then opens a dialog asking you to select the folder that contains your images (if it is always the same folder, simply define the path variable and omit the getDirectory("") command. It then creates an array with all the file names in that folder.
The for-loop iterates over all the file names in the list and the if indexOf() function inside the if statement checks whether the filename contains your search string (i.e. the indexOf() function returns a positive number). If that is correct, the image is opened. Otherwise, the file is ignored and the loop moves on to the next file name in the list.

Hope this helps,
Volko

searchString="00219";

path=getDirectory("");
list = getFileList(path);

for (i=0; i<list.length; i++) {
	if(indexOf(list[i],searchString)>0){
		open(path+list[i]);
	};
};