Batch convert series of Tiff images

fiji
imagej
batch-processing
macro
image-sequence

#1

Hello,

I have series of images from different ROIs saved as “Tiff” files in different subfolders. Each subfolder was named as “ROI00”, “ROI01” and the series of Tiff images in each subfolder always contain suffix “roi_00”, “roi_01”. I have been working with writing a macro to open series of images in each subfolder and change the canvas size, and then save the series of images as “tiff”. Below was my macro. Can somebody help me with this? Thank you.

input = getDirectory("Input directory");
output = getDirectory("Output directory");

processFolder(input);
function processFolder(input) {
	list = getFileList(input);
	for (i=0; i<list.length; i++) {
        if(File.isDirectory(input + list[i])==1);
           F= File.getName(+ input + "/" + list[i] +);
           run("Image Sequence...", "open= [" + input + list[i] + F"] starting=1 increment=1 scale=100 file=tif");
	       run("Canvas Size...", "width=5000 height=5000 position=Center");
	       run("Image Sequence... ", "format=TIFF save= [" + output + "] ");
       
	}
}

#2

Hello @catherineJY,

The main problem I found with your code is that when you come to try to import your image sequence, you’re just providing the name of the folder (F) not the filename of the first image in the folder as you would if you were running image sequence manually.

I’ve drafted an updated version that uses your base code but once it finds a directory, will do a second round of getFileList to poll the contents, sort this list and pull the first filename. This is then fed into Image Sequence... which seems to work fine.

Finally, to prevent possible filename clashes, I’ve added the subdirectory name to the output file name. In the code below, I’ve saved as a tif stack which seems tidier, but do as you please.

https://bitbucket.org/snippets/davemason/geRBqp

Hope that helps!

PS. Script Parameters are super helpful if you’re asking the user for parameters like input/output directories.


#3

Hello,

Thank you very much. That worked very well.

One more question for opening the image sequence that is only “tif” files. In my subfolders I typically have “tif” and “dm3” files. Unfortunately, the macro can pull both type of files. I wrote this to sort the “tif” files out, but not working.
run(“Image Sequence…”, “format=TIFF open=[”+input+list[i]+fileListFirst+"] sort");


#4

Have you tried using the Macro Recorder @catherineJY ?

I just ran an import on a test directory adding the filter .tif to the “File Name Contains” box like so:

Capture

This gives the following line in the macro recorder:

run("Image Sequence...", "open=C:/path/to/my/file/blobs1.tif file=.tif sort");

So the line you want is:

run("Image Sequence...", "open=["+input+list[i]+fileListFirst+"] file=.tif sort");

I’ve updated the snippet to include this change.


#5

Thank you! This worked well.