Export images with original names in TrakEM2

Tags: #<Tag:0x00007fd53f9796b0> #<Tag:0x00007fd53f9794d0>


Hi everyone, I am working with TrakEM2 to adjust images. Afterwards, I need to export flat color images but using their original names instead of z=0, 1, 2, etc. Can anyone give me a hint about the name changes (using original names as default)? Any help is appreciated.


Hi @Tian_Tian,

It sounds like the TrakEM2 is giving default names to each layer.
I think I can help, just want to get a feel for where you’re at. Some questions:

How are you importing your images into trakem2?
What are the title’s for each layer?
(see below for a macro that will print out the title of the first layer)

Is manually re-naming layers an option? If so, then you might get names like:

is this okay? Or do you need them to be exactly:


The macro:

import ij.IJ;
import ini.trakem2.Project;

title = Project.getProjects().get(0).getRootLayerSet().getLayers().get(0).getTitle();


Thanks a lot John (@bogovicj) ! I import images by dragging the whole folder into trackEM2, and titles are like “myname_0521” “myname_0714” etc. (the numbers at the end are not continuous dates). I hope they can be original names, but “06__myname5__z5.0.tif” is fine too.

Just a quick question, am I supposed to input the macro after exporting images from trackEM2 or before? If my images are from layer 0 to 5, do I need to replace ‘ij’ and ‘IJ’ by image names or layer? sorry I never worked with macro before, hope you don’t mind this basic questions.

Many appreciations.


Hi again @Tian_Tian,

Another question - after you drag the folder to TrakEM2, you import the directory as a Stack right? That is, after import, is it the case that there is one “image” or “patch” per layer?

This will help to make sure that the script I write will work correctly…



To follow up, I have something that may be workable for you.
For it to work you should:

  1. Import your trakem2 project as usual
  2. Do whatever processing you’re going to do.
  3. Run the script below in Fiji (see below)
  4. Export your images as usual

Here is the script:

import ij.IJ;
import ini.trakem2.Project;
import ini.trakem2.display.Patch;

project =  Project.getProjects().get(0);
layers =  Project.getProjects().get(0).getRootLayerSet().getLayers();

* For each layer in the current TrakEM2 project, change the title
* to the title of the "first" patch object in that layer.
for( int i=0; i < layers.size(); i++ )
	l = layers.get(i);
	newName = l.getDisplayables(Patch.class).get(0).getTitle();
	project.findLayerThing( l ).setTitle( newName );

To run it, Plugins > New > Macro in Fiji, then paste that code snippet in the window, and press Run. You may also need to do Language > Beanshell in the window you paste the code into.

That should do the trick, but when I tried it, the output were formatted like "06__myname5__z5.0.tif”

Removing the leading and trailing numbers would take more TrakEM2 hacking than I’m willing to devote - since, if I were in your shoes, I’d write a script to rename everything after export.

In any case, let us know if that works for you,


Thank you so much for your help! I did import my images as stack, because i want to do the image transform to align them.

I run the code, but somehow there is an error to recognize all ‘()’ ‘{}’, is this due to my fiji or else?

I really appreciate your helps!



Could you please post the exact text of the error you got?
Also, did you try Language > Beanshell in the macro editor window before running?



Yes I did change to Beanshell in Language. One silly question: do I need to change anything in the code you posted? Like the name of the project?

Sorry I just completely copied and pasted.


Not a silly question. No, you don’t have to change anything about the code. Copy-pasting it exactly as written above is the right thing to do.

Is it still not working? Could you post the complete text of the error you’re getting?


Yes the attached picture is the error. Many thanks.


this is .



I believe you just need to save @bogovicj’s script as .bsh since it’s a beanshell script… not .ijm. That should take care of that error you are getting.

eta :slight_smile:


Thank you @etarena for pointing it out! It works now.
Thank you @bogovicj for helping me solve this tricky problem, I really appreciate it.

Best wishes guys!