Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Created June 18, 2013 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrueden/5805462 to your computer and use it in GitHub Desktop.
Save ctrueden/5805462 to your computer and use it in GitHub Desktop.
A hack for calling the Bio-Formats Exporter programmatically.
import ij.IJ;
import ij.ImagePlus;
import ij.Macro;
import loci.plugins.LociExporter;
/**
* This demonstrates how to programmatically call the Bio-Formats Exporter
* plugin, working around ImageJ's limitation that macro-based executions must
* be run in a thread named with a "Run$_" prefix. There is likely a simpler way
* of doing it, but this solution works.
*/
public class ExporterWorkaround implements Runnable {
private ImagePlus imp;
public ExporterWorkaround(final ImagePlus imp) {
this.imp = imp;
}
@Override
public void run() {
final LociExporter bfExporter = new LociExporter();
final String file = "/Users/curtis/Desktop/clown.tif";
final String macroOpts = "save=[" + file + "] compression=LZW";
bfExporter.setup(null, imp);
Macro.setOptions(macroOpts);
bfExporter.run(null);
IJ.showMessage("All done!");
}
public static void main(String... args) {
// open a test image
final ImagePlus imp =
IJ.openImage("http://imagej.nih.gov/ij/images/clown.jpg");
// launch the Bio-Formats Exporter in a special thread
// NB: Must run in a thread with name starting with "Run$_"
// https://github.com/fiji/ImageJA/blob/v1.47t/src/main/java/ij/Macro.java#L88
final Thread t = new Thread(new ExporterWorkaround(imp), "Run$_Exporter");
t.start();
try {
t.join();
}
catch (final InterruptedException e) {
IJ.handleException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment