ImageJ and Variants

Distributions

Plugins

The following plugins may be of particular interest to our users:

Forums

Macros

Kari's 2020 webinar (box link here) has a basic introduction to macros. Below are some Basic Macro's that were writtenΒ  and last run on in Fiji, ImageJ v 1.53f51, Java 1.81-172 (64-bit).

To open: Download then drag and drop onto ImageJ/Fiji Tool Bar

πŸ”§ SaveLIFseries.ijm β€” Batch convert LIF files to OME.tif
/*this is a macro for batch processing LIF files in to ome.tif files.
 * Instructions:
 * Put your LIF files you want to convert into a folder. This is your Input/source directory
 * Create a folder for your files to be out put to. This is your output/destination directory
 * If you want to run any processes on the images while you are converting them you can change the code below where indicated.
 * When you are readt, click "Run"
 * It will prompt you to select your input folder, choose it and click select
 * It will then prompt you to click your output folder, choose it and click select
 * It should run and process all your files
*/
run("Bio-Formats Macro Extensions");
setBatchMode(true);

currentDirectory = getDirectory("Choose a Source Directory");
outputDirectory = getDirectory("Select Destination");

fileList = getFileList(currentDirectory);

for (file = 0; file < fileList.length; file++) {
    Ext.isThisType(currentDirectory + fileList[file], supportedFileFormat);
    if (supportedFileFormat=="true") {
        Ext.setId(currentDirectory + fileList[file]);
        Ext.getSeriesCount(seriesCount);
        for (series = 1; series <= seriesCount; series++) {
            run("Bio-Formats Importer", "open=[" + currentDirectory + fileList[file] + "] autoscale color_mode=Composite rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT series_"+series);
            name=getTitle();
            run("Bio-Formats Exporter", "save=[" + outputDirectory + name + ".ome.tif" + "] compression=Uncompressed");
            close();
        }
    } else if (endsWith(fileList[file], "/")) {
        processBioFormatFiles(currentDirectory + fileList[file]);
    }
}

setBatchMode(false);

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ ConvertND2-ColorOME.ijm β€” Convert BF Color RGB ND2 to OME.tif and reorder channels
// batchTiffConvert.txt

/*This converts BF Color RGB ND2 Files into BF Color RGB TIF files and re orders the channels
 * because NIS elements writes the colors in a Differnt header order than Fiji
*/
oneFilePerSlice = false;

directory = getDirectory("Input files");
fileList = getFileList(directory);
outputDirectory = getDirectory("Output directory");

run("Bio-Formats Macro Extensions");
setBatchMode(true);

for (i=0; i<fileList.length; i++) {
  file = directory + fileList[i];
  if (oneFilePerSlice) {
    Ext.setId(file);
    Ext.getImageCount(imageCount);
    for (image=0; image<imageCount; image++) {
      Ext.openImage("", image);
      outFile = outputDirectory + fileList[i] + "-" + image + ".ome.tiff";
      saveFile(outFile);
      close();
    }
    Ext.close();
  } else {
    Ext.openImagePlus(file);
    outFile = outputDirectory + fileList[i] + ".ome.tiff";
    saveFile(outFile);
    close();
  }
}

showStatus("Finished.");
setBatchMode(false);

function saveFile(outFile) {
    Stack.setChannel(1); run("Blue");
    Stack.setChannel(2); run("Green");
    Stack.setChannel(3); run("Red");
    run("Make Composite");
    run("RGB Color");
    name=getTitle();
    s=lastIndexOf(name, '.');
    name=substring(name, 0,s);
    run("Bio-Formats Exporter", "save=[" + outputDirectory + name + ".ome.tif" + "] compression=Uncompressed");
}
close();

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ ConvertND2toOMEremoveND2.ijm β€” Convert ND2 folder to OME.tif and strip file suffix
/*This converts a folder full of images (your input folder) into OME.tifs and puts them in a new output folder.
 * It is also set up to remove the suffix (i.e. ".ND2") from the image name just to clean it up a bit.
*/

oneFilePerSlice = false;

directory = getDirectory("Choose input files");
fileList = getFileList(directory);
outputDirectory = getDirectory("Choose output directory");

run("Bio-Formats Macro Extensions");
setBatchMode(true);

for (i=0; i<fileList.length; i++) {
  file = directory + fileList[i];
  if (oneFilePerSlice) {
    Ext.setId(file);
    Ext.getImageCount(imageCount);
    for (image=0; image<imageCount; image++) {
      Ext.openImage("", image);
      name = fileList[i];
      s=lastIndexOf(name, '.');
      name=substring(name, 0,s); 
      name=replace(name,".","_");
      outFile = outputDirectory + name + "-" + ".ome.tif";
      saveFile(outFile);
      close();
    }
    Ext.close();
  } else {
    Ext.openImagePlus(file);
    name = fileList[i];
    s=lastIndexOf(name, '.');
    name=substring(name, 0,s);
    name=replace(name,".","_");
    outFile = outputDirectory + name + "-" + ".ome.tif";
    saveFile(outFile);
    close();
  }
}

showStatus("Finished.");
setBatchMode(false);

function saveFile(outFile) {
   run("Bio-Formats Exporter", "save=[" + outFile + "] compression=Uncompressed");
}

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ BatchSplit-3channel-tif.ijm β€” Batch split 3-channel images into separate C1, C2, C3 tiff files
/* This is a batch processing macro that has been designed to process
 * a folder of images (your source directory) and to split a 3 channel image into 3 separate tiff files
 * naming them C1, C2, and C3 and save them in a separate specified folder.
 * You can comment out channels you do not need or add more if needed.
*/

sourcedir=getDirectory("Source directory with images");
destdir=getDirectory("Destination directory for images and data");

fileList=getFileList(sourcedir);

setBatchMode(true);
run("Bio-Formats Macro Extensions");

for (i=0; i<fileList.length; i++) {
    file = sourcedir + fileList[i];
    Ext.openImagePlus(file);
    outFile1 = destdir + "C1-"+ fileList[i];
    outFile2 = destdir + "C2-"+ fileList[i];
    outFile3 = destdir + "C3-"+ fileList[i];
    saveFile(outFile1);
}

setBatchMode(false);

function saveFile(outFile){
 run("Split Channels");
  selectWindow("C1-"+ fileList[i]);
    saveAs("Tiff",+ outFile1 + );
 selectWindow("C2-"+ fileList[i]);
    saveAs("Tiff",+ outFile2+ );
 selectWindow("C3-"+ fileList[i]);
    saveAs("Tiff",+ outFile3 + );
}

close();

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ BatchSplit-3channel-tif-fixed.ijm β€” Fixed version of the 3-channel batch split macro
/* This is a batch processing macro that has been designed to process
 * a folder of images (your source directory) and to split a 3 channel image into 3 separate tiff files
 * naming them C1, C2, and C3 and save them in a separate specified folder.
 * You can comment out channels you do not need or add more if needed.
*/

sourcedir=getDirectory("Source directory with images");
destdir=getDirectory("Destination directory for images and data");

fileList=getFileList(sourcedir);

setBatchMode(true);
run("Bio-Formats Macro Extensions");

for (i=0; i<fileList.length; i++) {
    file = sourcedir + fileList[i];
    Ext.openImagePlus(file);
    outFile1 = destdir + "C1-"+ fileList[i];
    outFile2 = destdir + "C2-"+ fileList[i];
    outFile3 = destdir + "C3-"+ fileList[i];
    saveFile(outFile1);
}

setBatchMode(false);

function saveFile(outFile){
 run("Split Channels");
  selectWindow("C1-"+ fileList[i]);
    saveAs("Tiff", outFile1);
 selectWindow("C2-"+ fileList[i]);
    saveAs("Tiff", outFile2);
 selectWindow("C3-"+ fileList[i]);
    saveAs("Tiff", outFile3);
}

close();

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ Batch-imageSeq-bioformats.ijm β€” Batch open with Bio-Formats and save as image sequence
/* This is a batch processing macro that has been designed to process
 * a folder of images (your source directory) by opening them with the
 * Bio-Formats importer and apply the commands found in the function.
 * This particular macro is set up to save the images as an image sequence.
*/

sourcedir=getDirectory("Source directory");
destdir=getDirectory("Destination directory");

fileList=getFileList(sourcedir);

setBatchMode(true);
run("Bio-Formats Macro Extensions");

for (i=0; i<fileList.length; i++) {
    file = sourcedir + fileList[i];
    Ext.openImagePlus(file);
    outFile = destdir + fileList[i];
    saveFile(outFile);
}

setBatchMode(false);

function saveFile(outFile){
}

run("Image Sequence... ", " dir=[" + destdir + "] format=TIFF ");
close();

β†— Open in new tab  |  ⬇ Download Macro

πŸ”§ Batch-imageSeq_CentralSelector-Bioformats.ijm β€” Batch open and select central Z-slices per stack
/* This is a batch processing macro that has been designed to process
 * a folder of images (your source directory) by opening them with the
 * Bio-Formats importer and apply the commands found in the function.
 * This file was modified by Francois Mifsud to find the central plane and select a specific region around it!
 * Modified by AB and FM on 05-03-21 to select the same number of slices at the center of each stack.
*/

sourcedir=getDirectory("Source directory");
destdir=getDirectory("Destination directory");
ntarget = 10;       // has to be even and lower or equal to the lowest number of slices in all images
nchannels = 3;      // enter number of channels in the images

fileList=getFileList(sourcedir);

setBatchMode(true);
run("Bio-Formats Macro Extensions");

for (i=0; i<fileList.length; i++) {
    file = sourcedir + fileList[i];
    Ext.openImagePlus(file);
    name = fileList[i];
    s=lastIndexOf(name, '.');
    name=substring(name, 0,s);
    name=replace(name,".ome","");
    name=replace(name,".","_");
    outFile = destdir + fileList[i];
    saveFile(outFile);
}

setBatchMode(false);

function saveFile(outFile){
    n = nSlices()/nchannels;
    if (n%2 != 0) {
        write("Number of slices is odd, dropped last slice");
        n = n-1;
    }
    binf = (n/2) - (ntarget/2) + 1;
    bsup = (n/2) + (ntarget/2);
    write("Original number of slices=" + n + ", Keeping slices "+ binf + "-" + bsup);
    run("Make Substack...", "channels=1-3 slices="+ binf + "-" + bsup);
    run("Bio-Formats Exporter", " save=[" + outFile + "] write_each_z_section write_each_channel export compression=Uncompressed");
    close();
}

β†— Open in new tab  |  ⬇ Download Macro

Resources for learning

Peter Bankhead: Analyzing fluorescence microscopy images with ImageJ

Kari's Webinar β€” Introduction to Fiji