Search code examples
macrosimagej

ImageJ: Open a series of text images as a stack


In ImageJ, I can open a series of images (tif, png, jpg, etc.) as a stack by using File>>Import>>Image Sequence...

And I can open a text image (.txt file containing a matrix of pixels) using File>>Import>>Text Image...

But how can I open a series of text images as a stack?

Some people have offered a Macro that does this, but it does not work very well, because it also opens all of the images individually and, if you have other images open, then those are incorporated into the stack as well (try running the macro twice in a row).

It would be nice if there was a macro or plugin that would import the text images into a single stack (ignoring other open images) and not also bring up the individual images. Basically, it should operate just like Import>>Image Sequence.

Here is the current macro as a starting point, though I'm not sure this is the best place to start. Thanks!

dir = getDirectory("Choose directory");
list = getFileList(dir);
run("Close All");
setBatchMode(true);
for (i=0; i<list.length; i++) {
   file = dir + list[i];
   run("Text Image... ", "open=&file");
}
run("Images to Stack", "use");
setBatchMode(false);

Solution

  • Personally the macro you pasted worked for me when run in the latest Fiji - the run("Close All"); line is intended to prevent unintended images from being included in your stack, while the batch mode prevents the intermittent images from popping up.

    In fact Images to Stack is supposed to complain if you try to run it with a non-2D image (i.e. the output of your previous run). So if you are seeing this behavior when running the macro, it could be a bug - can you provide more information about what version of ImageJ/OS/Java you're using?

    Also, a more general solution may be the Bio-Formats importer - which has a flag that allows you to group multiple files into a single stack. If you don't want to manually install it, Bio-Formats is included by default with the Fiji distribution of ImageJ

    You may also be interested in testing ImageJ2 functionality, which is intended to slot in full format support into existing functions - e.g. so that macros like this do not need to be tailored to individual formats. In theory, if you download Fiji and turn on Edit>Options>ImageJ2, then File>>Import>>Image Sequence... should simply work the way you wanted it to... but it's currently not well tested.