0
votes

I have several image stacks from a confocal microscope saved as .lsm, and I want to write a macro that:

  1. opens an image in a folder
  2. Makes a Composite, then Stacks to RGB, then sets animation options
  3. Saves this as an Animated Gif to another folder

What I made so far:

function stack_to_gif(input, output, filename, fps) {
    open(input + filename);
    run("Make Composite");
    run("Stack to RGB", "slices");
    run("Animation Options...",  "speed=fps");
    run("Animated Gif ... ", "name=title=[Do not use] optional=[] " +
    "image=[No Disposal] set=500 number=-1 transparency=[No Transparency] "     +
    "red=0 green=0 blue=0 index=0 filename=output+filename");
    saveAs("Animated Gif...", output+filename);
    close();
}

inputDir=getDirectory("Choose an input location");
outputDir=getDirectory("Choose an output location");
fps=120;

setBatchMode(true);
list = getFileList(inputDir);
for (i = 0; i < list.length; i++){
    stack_to_gif(inputDir, outputDir, list[i], fps);
}
setBatchMode(false);

I know that the two bottom parts work as I use them in another macro, too.

My problem is with the saveAs("Animated Gif...") part, as it does not allow me to save the RGB-types, even though it works when I do it manually.

What I tried so far:

  • use the same options of run("Animated Gifs...") within saveAs("Animated Gifs...")
  • use one or the other but not both
  • use run("8-bit"); after Animation Options, which works (but I lose the colors)

What irks me is that it works perfectly fine when I do it manually, but I get the error message

To save as Animated GIF, the stack must be converted to 8-bit indexed color by the Image>Type>8-bit Color command.

and I don't know why this doesn't happen when I do it myself.

Thank you for your help.

2
Questions concerning ImageJ usage and specific commands related to ImageJ and Fiji plugins are best addressed to the ImageJ forum directly, where they're more likely to be read and answered by ImageJ experts. - Jan Eglinger

2 Answers

1
votes

When I used the macro recorder (Plugins > Macros > Record...) to record the command corresponding to File > Save As > Animated Gif..., I get the following command:

run("Animated Gif ... ", "name=[my new name] set_global_lookup_table_options=[Do not use] optional=[] image=[No Disposal] set=20 number=-1 transparency=[No Transparency] red=0 green=0 blue=0 index=0 filename=/usr/temp/animated.gif");

In your command, the part name=title=[Do not use] seems to be wrong. If you want to use the current image title, use string concatenation to insert the content of a variable:

run("Animated Gif ... ", "name=[" + myTitle + "] set_global_lookup_table_options=[Do not use] optional=[] image=[No Disposal] set=20 number=-1 transparency=[No Transparency] red=0 green=0 blue=0 index=0 filename=/usr/temp/animated.gif");

But anyhow, the name parameter is not needed, since it will be overridden by the filename parameter containing the whole file path, so you can safely leave it away.

In the same way you can omit any parameter that is not different from the default value. In my tests, this worked just as well:

run("Animated Gif ... ", "set=20 filename=/usr/temp/animated.gif");

Note: When saving animated gifs, the playback speed is solely determined by the Set delay in milliseconds parameter (set=20 in my example), so running run("Animation Options...", "speed=fps"); will have no impact on the saved file. (See the source code. The wiki page seems to be outdated/wrong concerning this. Feel free to correct the documentation yourself, it's a wiki.)

1
votes

We had the same problem. You need to change the option in "set_global_lookup_table_options" from [Do not in use] to [Load from Current Image]. This function saves z-stacks in animated gif with 50us delay (set=50) and it's ok for the color-problem.

function animated_gifZ(input, output, filename)
{
open(input + filename);
run("RGB Color", "slices");
run("8-bit Color", "number=256");
run("Animated Gif ... ", "set_global_lookup_table_options=[Load from Current Image]" +
"optional=[] image=[No Disposal] set=50 number=-1 transparency=[No Transparency]" +
"red=0 green=0 blue=0 index=0 filename=output+filename");
saveAs("Animated Gif...", output+filename);
close();
}

If you want to speed it up you can just use:

function gifZ(input, output, filename)
{
open(input + filename);
run("RGB Color", "slices");
run("8-bit Color", "number=256");
run("Properties...", "frame=[0.1 sec]");
saveAs("Gif", output+filename);
close();
}

Hope it helps.