Specifying the ImageServer / Image provider / Server type (eg Bio-Formats or OpenSlide) in a QuPath script

I’m trying to write a script to generate tiles from a whole slide image, and I want to have control over which ImageServer I use to read the image. I have taken and very lightly adapted a test script from the QuPath documentation:

// script filename: dumptiles.groovy
import qupath.lib.scripting.QP

// Get the current image (supports 'Run for project')
def imageData = getCurrentImageData()

def name = GeneralTools.getNameWithoutExtension(imageData.getServer()
						.getMetadata().getName())

// Define output path (here, relative to project)
def pathOutput = buildFilePath('/tmp/QuPath-test', 'tiles', name)
mkdirs(pathOutput)

// Define output resolution in calibrated units (e.g. µm if available)
double requestedPixelSize = 5.0

// Convert output resolution to a downsample factor
double pixelSize = imageData.getServer().getPixelCalibration()
    .getAveragedPixelSize()
double downsample = requestedPixelSize / pixelSize

// Create an exporter that requests corresponding tiles from the
// original & labelled image servers
new TileExporter(imageData)
    .downsample(downsample)   // Define export resolution
    .imageExtension('.tif')   // Define file extension for original pixels
                              // (often .tif, .jpg, '.png' or '.ome.tif')
    .tileSize(512)            // Define size of each tile, in pixels
    .annotatedTilesOnly(false) // If true, only export tiles if there is a
                              // (classified) annotation present
    .overlap(64)              // Define overlap, in pixel units at the
                              // export resolution
    .writeTiles(pathOutput)   // Write tiles to the specified directory

println 'Done!'

I would then call this script using the command line:

QuPath-0.2.3 script -i /path/to/imagefile.svs dumptiles.groovy

Since this is not part of a QuPath project, there is no prior specification of the Server type so QuPath has to make the decision. I haven’t been able to yet figure out how to specify this in a script (similar to what one would do in the GUI frontend when importing an image and selecting the Image provider).

Any pointers would be gratefully appreciated!

Thanks

Hi @juliang, I can’t think of any way to do this that isn’t awkward… so here’s an example of an awkward way:

def server = getCurrentServer()
def uri = server.getURIs()[0]

//def server2 = new qupath.lib.images.servers.bioformats.BioFormatsServerBuilder().buildServer(uri)
def server2 = new qupath.lib.images.servers.openslide.OpenslideServerBuilder().buildServer(uri)

print "Original server: ${server}"
print "New server: ${server2}"

// Create an ImageData if you need one
def imageData = new ImageData(server2)

Basically, you let QuPath use whichever provider it likes and then just request the URI from that and create the one you want (uncommenting the line as needed).

I haven’t tried this from a command line, but hopefully it works.

2 Likes

Thanks @petebankhead - that worked perfectly!