Batch processing w/ bio-formats to open and save ".nd2" to ".ome.tiff" macro

Hello ImageJ forum!

I have several hundred z-stacks in .nd2 format and need to convert them using the bio-formats plugin to .ome.tiff files for processing in Comstat. I have tried to write a macro to do this automatically for me, instead processing one by one. I have only managed to solve this in by copy and pasting the code a hundred times and quickly changing the specific file name in each command using excel. But its not the most efficient way. Right now, I have this for loop but I can’t get it to work. I would really appreciate any help with troubleshooting! :slight_smile:

path = getDirectory("C:\\Users\\Annika\\Desktop\\48hNeoss\\"); 
outputFolder = getDirectory("C:\\Users\\Annika\\Desktop\\destination\\"); 
filelist = getFileList(path); //load array of all files inside input directory
 for (i=0; i< filelist.length; i++) {
     // process nd2 files only
     if (endsWith(filelist[i], ".nd2")) {
         // open file, requires LOCI tools (aka Bio-Formats)
         run("Bio-Formats Importer", "open= + path + filelist[i] color_mode=Default view=Hyperstack stack_order=XYCZT");
         run("Bio-Formats Exporter", "save= + outputFolder + filelist[i] + .ome.tif export compression=Uncompressed");
1 Like

Hi, @annikj, and welcome!

This page on processing multiple files might be helpful for revising the macro if you haven’t read it already.

It looks like the curly brackets are not closed in the for loop and the if statement.

Also, when you specify paths for open/save operations, you should put the + outside the quotes. Be sure to preserve a space between arguments within the quotes. Like so:

run("Bio-Formats Importer", "open=" + path + filelist[i] + " color_mode=Default view=Hyperstack stack_order=XYCZT");

Even more efficient might be the Bio-Formats command-line tools (particularly bfconvert), though I haven’t tried them so I can’t advise on proper usage.

If you still have problems, you can get more detailed help if you explain in what specific ways the macro doesn’t work – error messages, bad output, etc.

2 Likes

Indeed, it should work to run something like the following from the console:

On Windows:

for /r %f in (*.nd2) do bfconvert %f %f.ome.tiff

On Linux or OS X:

for f in *.nd2
do
  bfconvert $f $f.ome.tiff
done

See also http://imagej.net/Batch

2 Likes

Hi all,

I have been trying to convert my .lif files to .ome-tiff. I made modifications to your code; however I am running into a bug on the third line with getFileList(path) from a defined path. Are there any other solutions to go from .lif to .ome-tiff?? Thanks

path = getDirectory("ImageExpt_Chi"); 
outputFolder = getDirectory("/Users/labadmin/Desktop/From confocal”)
filelist = getFileList(path); //load array of all files inside input directory
 for (i=0; i< filelist.length; i++) {
     // process lif files only
     if (endsWith(filelist[i], ".lif")) {
         // open file, requires LOCI tools (aka Bio-Formats)
         run("Bio-Formats Importer", "open= + path + filelist[i] color_mode=Default view=Hyperstack stack_order=XYCZT");
         run("Bio-Formats Exporter", "save=” + outputFolder + filelist[i] + “.ome.tif export compression=Uncompressed");

Your macro has “smart quotes” in it. It is advised to disable the smart quotes feature in any external text editor you use for programming. Here is a version of your snippet without smart quotes. Note that I did not test it, though:

path = getDirectory("ImageExpt_Chi"); 
outputFolder = getDirectory("/Users/labadmin/Desktop/From confocal")
filelist = getFileList(path); //load array of all files inside input directory
 for (i=0; i< filelist.length; i++) {
     // process lif files only
     if (endsWith(filelist[i], ".lif")) {
         // open file, requires LOCI tools (aka Bio-Formats)
         run("Bio-Formats Importer", "open= + path + filelist[i] color_mode=Default view=Hyperstack stack_order=XYCZT");
         run("Bio-Formats Exporter", "save=" + outputFolder + filelist[i] + ".ome.tif export compression=Uncompressed");

Did you try the bfconvert one-liners I posted above? This is much simpler than writing an ImageJ macro.

2 Likes

Hi everyone,

I’m trying to create a for loop to open, process and save .nd2 files, but it already goes wrong with opening. This is the code I tried last:

for (i=1; 1<20; i++){
run(“Bio-Formats Importer”, “open=[C:\Users\tenni\Pictures\Microscopy\2017-10-31 Confocal Nikon (plate 24-10)\raw\C11\24-10 well C11 00” + i + “.nd2”] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
}

I tried many ways to get the ‘i’ into the file name correctly, but I did not find a solution. Can anyone help me?

Good day,

coding isn’t for everyone, it requires scrutiny.

Instead of

for (i=1; 1<20; i++){
…
}

try

for (i=1; i<20; i++){
…
}

Regards

Herbie

Hi Herby,

That is a very stupid mistake from me.
But changing it did not help, the error I get is that an ‘)’ is expected in line 3.

thanks for your very quick reply by the way!

Just try your code with a file that has a very simple name. If this works, try to find out what makes the original file name problematic.

Regards

Herbie

Does this

run("Bio-Formats Importer", "open=[C:\Users\tenni\Pictures\Microscopy\2017-10-31 Confocal Nikon (plate 24-10)\raw\C11\24-10 well C11 00" + i + ".nd2] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");

work for you?

Regards

Herbie

That almost works!
I did need the double backslashes in the directory, but just had to remove the quotes after .nd2. So it became:

for (i=1; i<20; i++){
run(“Bio-Formats Importer”, “open=[C:\Users\tenni\Pictures\Microscopy\2017-10-31 Confocal Nikon (plate 24-10)\raw\C11\24-10 well C11 00” + i + “.nd2] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT”);
}

Many thanks for helping me out!

Great that it works,

but I don’t see any difference to what I’ve proposed.

Regards

Herbie

Sorry, it is:

run(“Bio-Formats Importer”, “open=[C:\Users\tenni\Pictures\Microscopy\2017-10-31 Confocal Nikon (plate 24-10)\raw\C11\24-10 well C11 00” + i + “.nd2] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT”);

Thanks again and have a nice day!