Batch export of all tiles in the slide (for all slides in the project)

379 views
Skip to first unread message

Filip Bochner

unread,
Feb 19, 2019, 10:57:40 AM2/19/19
to QuPath users
Dear QuPath users,

I slightly modified Pete`s code which divides slides into tiles, and saves them in designated folder:
---------------------------
/**
 * Request an image from QuPath as an (ImageJ) ImagePlus.
 * The aim is to avoid the (arbitrary) size threshold in v0.1.2 of QuPath.
 *
 * @author Pete Bankhead
 */

import ij.IJ
import qupath.imagej.gui.IJExtension
import qupath.imagej.helpers.IJTools
import qupath.imagej.images.servers.ImagePlusServerBuilder
import qupath.lib.regions.RegionRequest
import qupath.lib.scripting.QPEx

// Access the relevant QuPath data structures
def imageData = QPEx.getCurrentImageData()
def server = imageData.getServer()

// Define how much to scale down - 1.0 means requesting the image at the full resolution
// (I'm defaulting to a large downsample because a small one often won't work...)
double downsample = 1.0
int size = 4000

for (i = 0; i*size<server.getWidth()-size; i++) {
    for (j = 0; j*size<server.getHeight()-size; j++) {
        // Request the region
        def region = RegionRequest.createInstance(server.getPath(), downsample, i*size, j*size, size, size)
       
        // Check if we are likely to have too many pixels for a Java array
        long w = (server.getWidth()/downsample) as int
        long h = (server.getHeight()/downsample) as int
        long maxPixels = Integer.MAX_VALUE - 5 // Approximate...
        // if (w * h > maxPixels) {
        //    downsample = Math.ceil(((long)server.getWidth() * (long)server.getHeight()) / maxPixels)
        //    print 'This image is going to be too big!  Requires ' + (w * h) + ' pixels, while I can handle at best ' + maxPixels
        //    print 'Try again setting the downsample to be at least ' + downsample + ' (and preferably more)'
        //    return
        //}
       
        // Request an ImagePlus
        server = ImagePlusServerBuilder.ensureImagePlusWholeSlideServer(server)
        def imp = server.readImagePlusRegion(region).getImage()
       
        // You could save the ImagePlus here if you don't need to show it...
        IJ.save(imp, 'G:\\User_Data2\\sxxxx\\qupath_export\\save_'+i+'_'+j+'.tif')
       
        // Make sure that ImageJ is open
        //IJExtension.getImageJInstance()
        //imp.show()
    }
}
-----------------------------------------

I would like to do the same for all the slides in the project. I think that "Run for project" function will be useful here, however I don`t know how to make sure that each tile has also the name of the slide included (if they are saved in the same folder I wouldn`t like to have them overwritten).

What would be even more helpful is to sort the tiles into separate folders, with names corresponding to original slide names.

I`m really new to coding, I would appreciate your help.

Thanks!

Filip


micros...@gmail.com

unread,
Feb 19, 2019, 12:04:44 PM2/19/19
to QuPath users
You have a few options:
See 
Export images by file name
and
Export images automatically for project

All you need to do to create extra folders is to also add the string for the file name into your file path. Those two scripts also have two different examples of how to build a file path!

Pete

unread,
Feb 19, 2019, 12:07:32 PM2/19/19
to QuPath users
Reply all
Reply to author
Forward
0 new messages