QuPath: Save Pixelsize when exporting Single TMA Cores?

Hello everyone,

to save individual TMA cores as single png images, I use a scrpit from Pete (Github Issue 97):

import javax.imageio.ImageIO
import qupath.lib.regions.RegionRequest

// Define resolution - 1.0 means full size
double downsample = 1.0

// Create output directory inside the project
def dirOutput = buildFilePath(PROJECT_BASE_DIR, 'cores')
mkdirs(dirOutput)

// Write the cores
def server = getCurrentImageData().getServer()
def path = server.getPath()
for (core in getTMACoreList()){
    // Stop if Run -> Kill running script is pressed   
    if (Thread.currentThread().isInterrupted())
        break
    // Write the image
    img = server.readBufferedImage(RegionRequest.createInstance(path, downsample, core.getROI()))
    ImageIO.write(img, 'PNG', new File(dirOutput, core.getName() + '.png'))
}
print('Done!')

My question is: how I can save the Pixelsize for example in a txt file for each core?

Best
Pablo

It seems easier to create a single text file, since all of the cores should have the same pixel size, but if you really want one per image, you would need to add in something like:

pixelSize = getCurrentImageData().getServer().getAveragedPixelSizeMicrons()
pixelSize = pixelSize*downsample
println(pixelSize)
File textFile = new File(dirOutput, core.getName()+'.txt')
textFile.write "Pixel size in microns "+pixelSize.toString()

In a bit of a rush, but you should be able to add that into your for loop, and it will dump a text file per image into the cores directory.

Edit for downsample.

You may also consider this script to save an ImageJ TIFF, which should have the pixel size information embedded in it: https://github.com/qupath/qupath/issues/202#issuecomment-411749897

Which is useful if you plan to do further analysis in ImageJ or Fiji, but perhaps not so useful if you’re using other software.

(Note: I didn’t actually test this again before posting it here… and am reasonably sure that it would only work for QuPath v0.1.2)