TrakEM2 scripting: export arealists as tiffs with template architecture



Hi all,

I have a big but simple set of segmented 2D TEM data - traced arealists of mitochondria and endoplasmic reticulum.

I have already sorted out the analysis of the exported arealist​ masks separately in python. However, I need to export some 300+ traces from TrakEM2 as tiff files for further processing and I want to stay organized (ie., retain the template architecture and blind-identifiers of the images/ROIs).

What I would like to do is export mitochondria arealists as “/-.tiff” (eg. “740/740-m1.tiff”), and ER traces as “/--ER.tiff” (eg. “740/740-m1-ER.tiff”).

I have been looking through the scripting examples, but haven’t quite figured out how to navigate the template tree and pull out the right information and traces for export.

Below I have uploaded a screenshot of my template with a sample of the objects and layers. To be clear, this is all 2D analysis, with layer/object names corresponding to blinded identifiers for the analyzed images.

Please let me know if any of you have any more resources or ideas for scripting beyond - really any help and feedback would be appreciated.



Hi @Sam_Weiser_Novak
and welcome to the forum.

I think I can help, but am a bit preocupied at the moment so expect me to be slow.

Your screenshot is great, but if you’re willing to share your project.xml as well, it would make helping much faster.

Best regards,


Thanks for your response @bogovicj.

Attached is a link to the xml file associated with the analysis.

To clarify, I want to export the area lists as tiffs, with filenames indicating their blind-identifiers (eg. 477) and their associated structure (mitochondria vs. RER vs. SER).

Thanks again,


Hi again @Sam_Weiser_Novak,

This beanshell script should do approximately what you want:

You’ll need to change the destdir variable. And you may want to change how the “prefix” is built if the file names don’t come out just as you like - this script builds a filename by concatenating the titles of all nodes of the project tree from the root to the AreaList

Hope this helps, and post back to let us know how it goes



Wow, this is awesome - thanks John! Really close to what I’m looking for - the only issue is that the output is cropped to it’s own boundaries, rather than to the input boundaries of the frame, so I lose the context of the relationships between the different areas.

For example, the output of the mitochondrial and RER layers, respectively, look as follows:

Clearly, the relationships between these two structures can’t be determined as their respective image dimensions are different.

How can we get output more similar to the native Export>Arealists as labels function? An example below:

Again, thanks so much for your help.



Hey @Sam_Weiser_Novak,

Ah! Got it. This should be a straightforward change - I’ll try this later.

Did you also want the output to be binary masks as in your examples below?


TrakEM2 : How to export arealist as label tiff in python scripting


Thanks for the quick response! It would be great to have both, if possible.

Thanks again.



Hi again @Sam_Weiser_Novak,

I had a look at this earlier, here’s what I found.
The relevant code in TrakEm2 is this:

This function is called by Export>Arealists as labels and itself has logic for naming AreaLists. I see two ways to get at what you want.

  • Option 1:
    • Reproduce this function, replacing the logic that creates file names.
  • Option 2:
    • Stick with my script above and pull out the part of this function that does the exporting.

I’ll have at this more tomorrow or over the weekend, but this may help if you decide to have a crack at it.



Hey again @Sam_Weiser_Novak,

Found some time to hack at this again. Give this one a try:

I think this addresses the above.


More details on what I did:
The first script I posted above used AreaList.getStack to generate the images to write out. This new script uses a modified* version of AreaList.exportAsLabels (linked to above) instead, but otherwise is the same as the first script.

*modified to create a mask for a single AreaList.


Update to that previous script, where the script above output a stack with as many sections as the trakem2 project had layers, the script below will only have as many sections in the stack as layers the arealist appears in.



Thought I’d wrap this thread up with the GibHub deployment of the final analysis approach.

Thanks again for your help.