Export masks as grid tiles?

Hi there,

First off this is really nice piece of software, and it is really amazing that it is free!

I have been searching around for how to import and export masks, and I have found some scripts that I have made work.

However I would really like one thing, that I have not been able to find an answer to:

  1. Being able to export my annotations as masks, for tiles from my grid. So let us say I have a 1024 x 1024 pixel grid (because that is what is needed for the machine learning I will apply afterwards), and I have annotated two of these tiles, and I then want to export two seperate 1024 x 1024 masks one for each tile. And I very much want these two tiles exported exactly as these two “grid” tiles. Does this maks sense? And is this doable and if so how?

You may be able to use the Tile exporter with defined tile size of your choice instead of the grid.

Look at this section of the docs.

You may have to add/remove class names in the example script based on your classification. Also check the value of multichanneloutput in the LabeledImageServer.

1 Like


And thanks for your input!

However I must say these “tiles” are quite confusing to me. And I do not really understand their location in the image from which they get generated.

Instead I found a walkaround of using Objects -> Annotations -> Specify Annotations, and then creating a rectangle that fits with the grid and is the desired size. I can then annotatate within this rectangle.

And then I can export my annotations using the script under “Individual annotations” from this page:

Where I just comment out the line with downsample and manually define it downsample to 1.0.


I am glad you found a workaround. :+1: If you haven’t looked at the keyboard shortcuts yet Ctrl+D will duplicate selected ROI when you want to make more than one same size ROIs.
I thought your annotations were bigger than one tile, in that case you would use Tile exporter on your big annotation, to break it in small tiles of fixed size.


Hi thanks for the tip!

My annotations can be bigger than one tile, it is just still a bit confusing to me exactly how the tiles are created? Also that I have to specify their size in micrometers instead of pixels, which is what I want. But it might be me missing something… But I like to be able to control the coordinates of what gets exported.

The tiles are in pixels, at least in the link provided above.

Either way should be fine, though, as you can define your tiles by pixel size either way. I think using annotation tiles would be easiest for moving objects back and forth, and that is what I have used in the past (though the tile exporter wasn’t available then). I think the TileExporter is more for “I have a whole tissue or other large annotation with sub-annotated regions inside of it, make me a lot of 1024x1024 image blocks covering that area.”
If you only want specific sub-areas, annotations are likely the way to go.

The main documentation for the TileExporter class is here:

which complements the bit on using the TileExporter with annotations.

Tiles start at the top left corner of the image. The tile size is defined in pixels (at the export resolution), but you also need a downsample value. This is the amount the scale down the image. The downsample can optionally be calculated from a requested pixel size in microns, which is useful if you want to ensure a comparable resolution when applied to images that might have been acquired with different magnifications / pixel sizes.

Exporting tiles that cover an entire image with/without overlap at any arbitrary resolution ends up being quite complicated, because of the potential for rounding errors. The TileExporter makes this easier.


Hi again,

And thanks for the feedback. When I run the “Tile Exporter” script, it kind of cuts my region (that is 1024x1024) into these weird tiles, that each only overlaps somewhat with my region…

But another problem when I do it my way, I get a .png file with 4 different colours. But I think rather instead I should 3 or 4 (depending of if you have 3 classes and background or 4 classes that cover evertyhing) binary masks. So is there a way around this problem? Or how exactly should a mask look as .png file?

The idea is that I would be able to import this mask as an annotation into QuPath.