macro 'Import ND2 Series' { /* 25.10.16 23:09 https://sils.fnwi.uva.nl/bcb/objectj/examples/Import_ND2_files * - converts ND2 series file into individual TIFF timelapse stacks. * - the macro asks to select a .nd2 file. * - if more .nd2 files are in the same directory, * a dialog allows to convert all of them * - each series is saved as one tiff. * - the TIFFs are saved in a folder named after the "parent" .nd2 file * see also: Martin Hoehne, August 2015 * http://forum.imagej.net/t/produce-max-projections-from-lif-files-in-imagej/93 * simplified by: Norbert Vischer, Oct 2016 */ run("Close All"); run("Bio-Formats Macro Extensions"); srcFullPath = File.openDialog("Select a File"); srcDir = File.getParent(srcFullPath); names = newArray(1); names[0] = File.getName(srcFullPath); allFiles = getFileList(srcDir); len = allFiles.length; allNd2Files = newArray(len); ndCount = 0; print("\\Clear"); print("dir = ", srcDir); print("---"); for(jj = 0; jj < allFiles.length; jj++) if(endsWith(allFiles[jj], ".nd2")){ allNd2Files[ndCount++] = allFiles[jj]; print(allFiles[jj]); } allNd2Files = Array.trim(allNd2Files, ndCount); if(ndCount > 1){ Dialog.create("Convert .nd2 to tif"); Dialog.addMessage("Selected File is :\n" + names[0]); Dialog.addMessage("Total number of .nd2 files is: " + ndCount); items = split("This One,All", ","); Dialog.addRadioButtonGroup("Convert:", items, 2, 1, "This One"); Dialog.show; doAll = (Dialog.getRadioButton == "All"); if(doAll) names = allNd2Files; } nTifs = 0; for (nn = 0; nn < names.length; nn++){ srcFullPath = srcDir + File.separator+ names[nn]; Ext.setId(srcFullPath);//-- Initializes the given path (filename). Ext.getSeriesCount(seriesCount); //-- Gets the number of image series in the active dataset. path = srcFullPath;//must be this var name for (j=1; j<=seriesCount; j++) { nTifs++; run("Bio-Formats", "open=path autoscale color_mode=Default view=Hyperstack stack_order=XYCZT series_"+j); name=File.nameWithoutExtension; simpleName = "series_" + j + ".tif"; rename(simpleName); if(j==1){ destDir = srcDir + File.separator+ name+"_tiffs"; if (File.exists(destDir)==false) File.makeDirectory(destDir); // new directory for tiff } destPath = destDir+File.separator+simpleName; //change seconds to minutes interval = Stack.getFrameInterval(); Stack.setFrameInterval(interval); Stack.getUnits(dummy, dummy, dummy, TimeUnit, dummy); if(TimeUnit == "s" || TimeUnit== "sec"){ secs = Stack.getFrameInterval(); Stack.setFrameInterval(secs/60); Stack.setTUnit("min"); } saveAs("TIFF", destPath); run("Close All"); call("java.lang.System.gc"); //garbage collector } run("Close All"); } print("\n--- Conversion finished ---"); print("Number of .nd2 files: ", ndCount); print("Number of created tif files: ", nTifs); selectWindow("Log"); }