Exporting registered tiff stack from TrakEM2

imagej
trakem2
#1

Hello,
I would like to export a registered tiff stack from Trak EM2. Can anyone give a tip on how to do this?

Freya

#2

Hello @freya and welcome to the ImageJ forum!

I believe you just have to right-click in the canvas, then Export > Create flat image…

1 Like
#3

Thanks for the tip! I don’t want to flatten the image. It’s a stack of 1684 serial sections that I registered and now I want to export as a tiff stack.

#4

Hi @iarganda,

I tried your suggestion and the stack that was originally 3.4 GB is now 104 GB. Is there a way to crop the project so it only includes the slices and not all of the black space?

Thanks!

Freya

#5

Following @albertcardona suggestions, you should be able to crop the area to export using a ROI before clicking on “Create flat image”.

#6

Freya,

There are options to skip black sections.

The right-click menu has also options to automatically adjust the canvas size to the visible images.

And as @iarganda said, you can draw an ROI and the export will be limited to it.

If you need more fine-grainded exports, it is easy to do with scripts. See for example the section titled “Create a snapshot in 8-bit, 16-bit, 32-bit or RGB”:

1 Like
#7

Dear all,
Can someone suggest how to script the export of ALL slices from trakEM2 in PY?
I’ve tried the suggested scripting example, but it exports only the visible slice, so I need help creating the loop and building the output stack OR writing the files to disk. [no python experience to speak of…]
This seems a question asked often. Would it make sense to have it as a permanent option in trakEM “make flat image”? It would be great to at least have a POST with the proper script.
I added the 1st line to avoid err mssg about not knowing what ImagePlus is. Would it be as simple as changing to ImageStack vs ImagePlus? [not sure that exists in trakEM structure]

from ij import IJ, ImagePlus	# need this to avoid err mssg
from ini.trakem2.display import Display, Patch
from java.awt import Color
 
front = Display.getFront() # the active TrakEM2 display window
layer = front.getLayer()
tiles = front.getSelection().get(Patch)  # selected Patch instances only
backgroundColor = Color.black
scale = 1.0
 
roi = tiles[0].getBoundingBox()
for tile in tiles[1:]:
  roi.add(tile.getBoundingBox())
 
print "Creating flat image from", len(tiles), "image tiles"
 
ip = Patch.makeFlatImage(
           ImagePlus.GRAY16,
           layer,
           roi,
           scale,
           tiles,
           backgroundColor,
           True)  # use the min and max of each tile
 
imp = ImagePlus("Flat montage", ip)
imp.show()

______end______
#8

Iterating over all layers:

(Change the /path/to/folders/ part.)

from ij import ImagePlus
from ini.trakem2 import Project
from ini.trakem2.display import Patch
from ij.io import FileSaver
from java.awt import Color

project = Project.getProjects()[0]
layerset = project.getRootLayerSet()

roi = layerset.get2DBounds()
scale = 1.0
backgroundColor = Color.black

# NOTE: EDIT THIS PATH
targetDir = "/path/to/folder/"

for i, layer in enumerate(layerset.getLayers()):
  print layer
  # Export the image here, e.g.:
  tiles = layer.getDisplayables(Patch)
  ip = Patch.makeFlatImage(
           ImagePlus.GRAY16,
           layer,
           roi,
           scale,
           tiles,
           backgroundColor,
           True)  # use the min and max of each tile

  imp = ImagePlus("Flat montage", ip)
  FileSaver(imp).saveAsTIFF(targetDir + str(i + 1) + ".tif")
2 Likes
Export 16-bit images below ROI from TrakEM2 project
#9

Fabulous! Many thanks and much appreciated.

#10

How do I use this script?

I’ve gone to Plugins->New->Macro. Pasted this script in the window. Then set Language->Python. It fails with this error:

AttributeError: ‘ij.io.FileSaver’ object has no attribute ‘saveAsTIFF’
Am I doing something wrong? I’m using Fiji 1.52e on Windows 7.

Wouldn’t this be an extremely useful function to have in the right click Export menu?

#11

Hi @esiefker,

Try changing saveAsTIFF to saveAsTiff.
Hopefully that should be it…

John

1 Like
#12

Thanks, that did get the script to run. I guess I didn’t understand the difference between layers and patches though. Is there a script that will do this for all the patches in a layer?

1 Like
#13

I think the script above should produce output for all patches in all layers, though sounds like you’re not getting what you expect. Could you please explain in more detail (adding screenshots if necessary).

John

#14

@esiefker,

Sorry, I missed your other post:

Let’s continue the conversation there.
John

1 Like