Variable output pathway in macro possible?

Hello

I have written the attached macro Test.ijm using the export function.

//@File(label="choose a directory", style="directory") directoryFromDialog

setOption("BlackBackground", true);
roiManager("reset");	
//necessary because the ROI Manager has a known problem in batch mode, This opens and visualizes it, this solves this issue

chosenDirectory = directoryFromDialog + File.separator;

setBatchMode(true);

processFiles(chosenDirectory);	//necessary to run the function

//clean up final open windows
close("Results");
close("ROI Manager");

setBatchMode(false);

//define the function
function processFiles(currentDir)
{

	fileList = getFileList(currentDir);
	
	//make an analysis subfolder to collect the data from the analysis
	outputDirName = currentDir + "Export";
	
	//make unique analysis folder if one is existing already
	folderCount = 1;
	while(File.exists(outputDirName)) 
	{
		outputDirName = currentDir + "Export-" + folderCount;
		folderCount++;
	}
	
	outputDir = outputDirName;
	File.makeDirectory(outputDirName);
	
	for(i=0; i<fileList.length; i++) 
	{
		//if the file encountered is a Tiff image file do the following
		if(endsWith(fileList[i], ".tif")) 
		{
			open(currentDir+fileList[i]);
			//do the processing and export the data
			originalImageName = getTitle();

			run("Macro...", "code=[if (v==0) v=12] stack");	
			
			selectWindow(originalImageName);
			run("Image...  ", "outputfile=&outputDir + &originalImageName");
		


		} 
		else if (endsWith(fileList[i], "/") && !matches(fileList[i], ".*Export.*")) 
		{
		
			processFiles(currentDir + fileList[i]);	
	    
		}
	}
}	

I used the same structure of the macro sucessfully with the function save() but i need the extended data from the function Export Image… to do the further processing.

Datatype.pdf (198.8 KB)

The data appears if you stay with the mouse a while on the image icon in the folder.

The issue is, that the output pathway of the function is not set by the variables. The images are overwritten in the original folder. How is it possible to use an output pathway defined by variables with the function Export Image…?

I hope you have all needed informations, otherwise do not hesitate to ask.

Thank you for your help.

Hi Bolle9232,

It looks like you’re walking the directory by recursively calling processFiles function. That’s a neat trick I haven’t seen before.

Just a couple of thoughts.

Cheers,

Chris

Saving the modified images

After the macro runs it modifies the image (a new window is not created). I would save modified image like this

	for(i=0; i<fileList.length; i++) 
	{
			open(currentDir+fileList[i]);
			//do the processing and export the data
			originalImageName = getTitle();

			run("Macro...", "code=[if (v==0) v=12] stack");	 

            // save the modified image to new folder file 
            save( outputDir + File.separator + fileList[i]);

            // close the current image 
            close();            
        }

Speeding up processing

The macro command you’re using is very slow as it it stepping through each pixel individually - something which is very slow in .ijm macros.

If will be much faster to use whole image operations in Fiji as these use compiled operations. Rather than using the run("Macro...",...) this does the same thing, but much faster

run("Duplicate...", " ");     // Make a copy of the image
setThreshold(0, 0);           // Select the pixels that are 0
run("Convert to Mask");       // Make a mask
run("Create Selection");      // turn this into an roi
close();                      // close the mask
run("Restore Selection");     // Select the pixels in the original image
setColor(12);                 // set the fill value to 11
fill();                       // fill the selection

To extend it to stack you just need to loop over the slice index.

1 Like

Hi Chris

Thank you for your comment. I like your idea to speed the whole process up. I will try this.

In other macros I used the command

save( outputDir + savename)

with savename as variable filename and it worked. And of course it is much faster. The issue is, that the function does not provide the metadata i need to use the images with the software i prepare the images for. The pdf shows the differences. I am not able to open the images saved by the command save() or saveas() with the other software but I can open the images from the command export image…().

Unfortunately I can not use the variable pathway like in the save command.

Cheers,

Bolle9232