Created
June 18, 2013 13:43
-
-
Save ctrueden/5805462 to your computer and use it in GitHub Desktop.
A hack for calling the Bio-Formats Exporter programmatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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