ImageJ macro: open the corresponding file

Hi everyone,

I am a new user of the ImageJ, which is necessary for my data analysis. Writing a macro would spare a lot of “same” work, since I have a big sample size to analyze. However, this is also a very new field for me :smiley:

So, I open an image and the macro has to analyze particles in defined ROIs. These ROIs are manually drawn and saved already in a zip file.
Now I want that the macro opens the ROI zip file A1 when the image A1 is open, or it opens ROI zip file A2 when image A2 is open. Can I make for instance an if loop which recognizes the title (or the 2 first positions A1, B1, B2 etc.) of the image and the zip folder.
Which commands do I have to use?

I’d be really happy for your help.



It’s not so straightforward… you could write your own function to do this, such as:

#@ File(label="Image", description="Select the image you want to currently work on") image
#@ File (label = "Output directory for ROI files", style = "directory") outputRois

openROI(image, outputRois);

function openROI(image, outputRois) {
	// open image using Bio-Formats
	run("Bio-Formats", "open=[" + image +"] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
	name = getTitle();
	listRois = getFileList(outputRois);
	for (i = 0; i < listRois.length; i++) {
		if(startsWith(listRois[i], name)) {
			run("ROI Manager...");
			roiManager("Open", outputRois + File.separator + listRois[i]);

Where image is your current image file and outputRois is the folder with your zip files (that contain the image name) - both are declared as a Script Parameter at the top of your macro code.

I’m not sure if this code will work in your particular case… it’s hard to tell without testing more. But here are some helpful links for Scripting with ImageJ/Fiji that can help you better edit it yourself:


What @etadobson posted is great! I was mocking something up quick using the batch process folder template so I will go ahead and add that. If you have a corresponding zip file for every tiff this should work.
Running split("A1.tif", "."); will give you this list: ["A1", "tif"] which you can use to open the corresponding zip file.

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

// See also for a version of this code
// in the Python scripting language.


// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	imtitle = split(file, ".");
	roiname = imtitle[0] + ".zip";
	open(input + File.separator + file);
	roiManager("Open", input + File.separator + roiname);
	run("From ROI Manager");
	saveAs("Tiff", output + File.separator + imtitle[0] + "_overlay.tif");
	run("Close All");
	selectWindow("ROI Manager"); 
1 Like