QuPath: script for exporting all annotations in a project + cropping

Hi there,

I tried to define ROIs on a series of images thanks to a scripts for image alignment & object transfer posted on this forum, and I wondered if it was possible to export all ROIs of a project as ome.tiff instead of doing it manually.
Also the tiffs are quite heavy and it takes a lot of time to export them; if there is a way to just crop the original image so as to keep only a given region instead of exporting them, it would be really helpful.

Thanks in advance !

Hi @Egl,

Could you clarify what you want to export? Some users want the raw data, some want the objects overlayed on top. Either way, the scripts for exporting images across a project are mostly shown here:
https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_images.html
And here:
https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_annotations.html
If you only want to export the area bounded by an annotation.

Cheers,
Mike

3 Likes

Hi @Mike_Nelson,
Thank you for your reply and sorry for this late answer.
Based on the scripts from the first link, I tried to write my own to export all the regions within a project:

def server = getCurrentServer()

def baseImageName = getProjectEntry().getImageName()

path = buildFilePath(PROJECT_BASE_DIR, 'ROIsTif')
mkdirs(path)

getProject().getImageList().each{
        println("Working on: "+it)
	def roi = getSelectedROI()
	def requestROI = RegionRequest.createInstance(server.getPath(), 1, roi)
	writeImageRegion(server, requestROI, 'path/' +it + '.tif')

}
print 'Done!'

I’ll keep trying to see if I can make it work.

What I wanted to do was to export an area bounded by an annotation in all images within a project, or directly crop this area (which is the only part to be analyzed) to save time.

What you should be getting from that script is all crops from the current image that is open, and ROIs from every part of the project. I would recommend not using the getProject() type of loop in your case and instead using “Run for project.”
image
Here the server used within the project loop is always the current server, the one that you have open in the viewer.

If you have more than one ROI per image, you may want to use something like it’s XY coordinates or increment a variable like “i” to change the file name slightly per ROI within the same image.

1 Like

I re wrote the script to do roughly what I described. There were a few other issues as well, for example you were not using the path variable that you defined at the top of your script anywhere* other than to create a folder.

def server = getCurrentServer()

def baseImageName = getProjectEntry().getImageName()

path = buildFilePath(PROJECT_BASE_DIR, 'ROIsTif')
mkdirs(path)

getAnnotationObjects().eachWithIndex{it, x->
        println("Working on: "+it)
	def roi = it.getROI()
	def requestROI = RegionRequest.createInstance(server.getPath(), 1, roi)
	currentImagePath = buildFilePath(PROJECT_BASE_DIR, 'ROIsTif',baseImageName +it+'_'+ x + '.tif')
	writeImageRegion(server, requestROI, currentImagePath)
}
print 'Done!'
1 Like

Hi @Mike_Nelson,
Thank you for this script.
It works fine, but I noticed that the resolution was decreased as compared to original image, even with a downsampling of 1. I changed “.tif” to “.ome.tif” it doesn’t change anything. Is there an equivalent to the “Default (lossless)” compression type as in manual exportation to ome.tif ?
Thank you in advance,
Eglantine

I am afraid I do not know, I have never noticed any decrease in image quality in most of my images unless the image itself was rotated. If the image is rotated, then you definitely cannot get the same resolution, but if only annotations are being rotated I do not know what the problem is in your case.

You may need to provide more specifics, an example, or metadata - but the image creation is not really specific to the script - it is part of QuPath. Maybe @petebankhead will have some idea once you provide more information about the exports.

Sorry, I should have tested it more times before posting here.
Actually it usually works well, but I get some bugs sometimes:

or blurry ome.tiff output.
I don’t know if it is due to the size of the image (~ 1Go) or of the ome.tif ROI (>2 Go). I will check if running for project (6 images of these) results in RAM saturation.
In any case, I am already very happy with this solution. Thanks for everything.

1 Like