Exporting Labelled Annotations as PNG Problems

Hello everyone!

I’m trying to export these labelled annotations as PNGs from a whole slide image:


The light green annotations are labelled ‘Stroma’ for example. I saw the following script on a similar related post:


import qupath.lib.images.writers.ImageWriterTools
import qupath.lib.regions.RegionRequest
import qupath.lib.gui.scripting.QPEx
import javax.imageio.ImageIO

def path = buildFilePath(PROJECT_BASE_DIR, ‘ROIs’)
mkdirs(path)
def name = getProjectEntry().getImageName()

def imageData = QPEx.getCurrentImageData()
def server = imageData.getServer()

i = 1
for (annotation in getAnnotationObjects()) {
roi = annotation.getROI()

def request = RegionRequest.createInstance(imageData.getServerPath(),
1, roi)

String tiletype = annotation.getParent().getPathClass()

if (!tiletype.equals(“Stroma”)) {

   String tilename = String.format("%s_Region%d.png", name, i)

   ImageWriterTools.writeImageRegion(server, request, path + "/" + tilename);
   print("wrote " + tilename)

   i++

}

}
print(“Done!”)


The error is:
ERROR: Error at line 27: Unable to write C:\Users\Ayman\Documents\College Life\Year 4\Project\QuPath\ROI\ROIs/#8_Sternum.SCN - Series 1_Region1.png! No compatible writer found.

I see that the last front-slash is a problem, but the script doesn’t seem to recognise back-slashes. Anybody have a workaround to this? I’m using Qupath-m8.

If you aren’t fixed to M8, you might want to look into the options provided here:
https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_images.html
or the corresponding exporting annotations section (masks etc).

I am never quite sure what people mean when they say labeled (the file name? alpha channel? color masks?), but another option was listed here:

And it does always help to put your code in coding format (preformatted text option) so that it can be copied and pasted out of the forum. If the code came from a post, knowing which version of QuPath that was for would help too. Much of the image writing functions changed, I think, for M9.

Hey thank you for your very quick reply! I’m using m8 for many other scripts, so I’d like to see if its doable before I try going back another version. By labelling I mean setting the class of a certain annotation. So I’d like to only save the annotations as images under that class. I’ve implemented two scripts and get the same error:

The script is in .groovy format implemented in QuPath-m8.

import qupath.lib.images.writers.ImageWriterTools
import qupath.lib.regions.RegionRequest
import qupath.lib.gui.scripting.QPEx
import javax.imageio.ImageIO
def path = buildFilePath(PROJECT_BASE_DIR, 'ROIs')
mkdirs(path)
def name = getProjectEntry().getImageName()
def imageData = QPEx.getCurrentImageData()
def server = imageData.getServer()
i = 1
for (annotation in getAnnotationObjects()) {
   roi = annotation.getROI()
   def request = RegionRequest.createInstance(imageData.getServerPath(), 
       1, roi)
   String tiletype = annotation.getParent().getPathClass()
   if (!tiletype.equals("Stroma")) {
       String tilename = String.format("%s_Region%d.png", name, i)
       def pathOutput = buildFilePath(path, tilename)
       ImageWriterTools.writeImageRegion(server, request, pathOutput) ;
       print("wrote " + tilename)
       i++
   }
}
print("Done!")
 

As well as the script you’ve linked to me (I’ve replaced getDetectionObjects with getAnnotationObjects):

Preformatted text`import qupath.lib.images.writers.ImageWriterTools
import qupath.lib.regions.RegionRequest
import qupath.lib.gui.scripting.QPEx
import javax.imageio.ImageIO
// Get the current image (supports 'Run for project')
def imageData = getCurrentImageData()
def server = getCurrentServer()
// Define output path (here, relative to project)
def name = GeneralTools.getNameWithoutExtension(imageData.getServer().getMetadata().getName())
/*******************
CHANGE THIS
*******************/
def pathOutput = "C:\\Users\\Ayman\\Documents\\College Life\\Year 4\\Project\\QuPath\\ROI\\ROIs\\" +name + "\\"
//def pathOutput = "G:\\Tiles\\"+name+"\\"
//Define what you want to export here
tiles = getAnnotationObjects().findAll{it.getPathClass() != 'Stroma'}
mkdirs(pathOutput)
i=1
for (tile in tiles){
    def requestFull = RegionRequest.createInstance(server.getPath(),1,tile.getROI())
    x = tile.getROI().getCentroidX()
    y = tile.getROI().getCentroidY()
    tileClass = tile.getPathClass().toString()
    fileName = pathOutput+"Tile "+"x "+x+" y "+y+" "+tileClass+" "+(i++)+".tif"
    //print describe(requestFull)
    writeImageRegion(server, requestFull, fileName)
    }
    print("Done!")

I always get the same error:

ERROR: Error at line 28: Unable to write C:\Users\Ayman\Documents\College Life\Year 4\Project\QuPath\ROI\ROIs#8_Sternum.SCN - macro_Region1.png! No compatible writer found.

Or from the second script:

ERROR: Error at line 26: Unable to write C:\Users\Ayman\Documents\College Life\Year 4\Project\QuPath\ROI\ROIs#8_Sternum.SCN - macro\Tile x 1000.0 y 5000.0 null 1.tif! No compatible writer found.

Does the writeImageRegion function work for m8?

As far as I can remember, that issue usually comes up when the image is not brightfield, and someone is trying to write it into a brightfield format (like png or jpeg).


I would check if your image is fluorescent (regardless of whether the image itself is, is that how you have it set in the Image tab in QuPath).

That was it!!! It didn’t work immediately, but after restarting QuPath and reloading the pictures as brightfield it worked! Thank you very much for your continuous support on this forum! It’s really appreciated :slight_smile:

Here is the final working script for me:


import qupath.lib.images.writers.ImageWriterTools
import qupath.lib.regions.RegionRequest
import qupath.lib.gui.scripting.QPEx
import javax.imageio.ImageIO

def path = buildFilePath(PROJECT_BASE_DIR, 'ROIs')
mkdirs(path)
def name = getProjectEntry().getImageName()

def imageData = QPEx.getCurrentImageData()
def server = imageData.getServer()

i = 1
for (annotation in getAnnotationObjects()) {
   roi = annotation.getROI()
  
   def request = RegionRequest.createInstance(imageData.getServerPath(), 
       1, roi)
   
   String tiletype = annotation.getPathClass()
   //Annotation class name
   if (tiletype.equals("Stroma")) {
   
       String tilename = String.format("%s_Region%d", name, i)
       def pathOutput = buildFilePath(path, tilename + '.png')
       ImageWriterTools.writeImageRegion(server, request, pathOutput);
       print("wrote " + tilename)
   
       i++
       
   }

}
print("Done!")


1 Like