Batch Converter

Author: Wayne Rasband (wsr at nih.gov)
History: 2000/12/14: First version
2000/12/14: Works more reliably from the command line
2003/01/27: Adds option to convert to 8-bit TIFF
2004/12/29: Added BatchConvert macro
2008/04/15: Updated to use IJ.getDiretory(), IJ.openImage() and IJ.saveAs()
Source: Batch_Converter.java
Limitations: Conversion of 16 and 32 bit images into BMP fails with ImageJ 1.40d and earlier.
Installation: Download Batch_Converter.class to the plugins folder and restart ImageJ.
Description: Converts a folder of images in any format supported by ImageJ's File>Open command (TIFF, JPEG, DICOM, GIF, PNG, PGM, BMP, FITS, ZIP Compressed TIFF) to TIFF, 8-bit TIFF, JPEG, GIF, PNG, PGM, BMP, FITS, Text Image, ZIP or Raw format. The plugin displays three dialogs. In the first, select the source folder. In the second, select the format you want to convert to. In the third, select the destination folder.

This plugin can be run from the command line using commands something like this:

    java -cp ij.jar;. Batch_Converter c:\dir1\ c:\dir2\ (Windows)
    java -cp ij.jar:. Batch_Converter /dir1 /dir2 (Unix)

To use a plugin to open the files (e.g., Analyze_Reader), use the -Dplugins.dir=path option to specify the location of the ImageJ directory (requires v1.37n or later):

    java -cp ij.jar;. -Dplugins.dir=c:\ImageJ Batch_Converter c:\dir1\ c:\dir2\ (Windows)
    java -cp ij.jar:. -Dplugins.dir=/Applications/ImageJ Batch_Converter /dir1 /dir2 (OS X)

Here is what BatchConvert, the macro version of this plugin, looks like:

  dir1 = getDirectory("Choose Source Directory ");
  format = getFormat();
  dir2 = getDirectory("Choose Destination Directory ");
  list = getFileList(dir1);
  setBatchMode(true);
  for (i=0; i<list.length; i++) {
     showProgress(i+1, list.length);
     open(dir1+list[i]);
     if (format=="8-bit TIFF" || format=="GIF")
        convertTo8Bit();
     saveAs(format, dir2+list[i]);
     close();
  }
 
  function getFormat() {
       formats = newArray("TIFF", "8-bit TIFF", "JPEG", "GIF", "PNG",
          "PGM", "BMP", "FITS", "Text Image", "ZIP", "Raw");
       Dialog.create("Batch Convert");
       Dialog.addChoice("Convert to: ", formats, "TIFF");
       Dialog.show();
       return Dialog.getChoice();
  }

  function convertTo8Bit() {
      if (bitDepth==24)
          run("8-bit Color", "number=256");
      else
          run("8-bit");
  }

|Plugins | Home |