Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active November 1, 2023 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/5067be4d5bb953339a82422aeeed02ff to your computer and use it in GitHub Desktop.
Save lacan/5067be4d5bb953339a82422aeeed02ff to your computer and use it in GitHub Desktop.
[Resave pyramid with fixed bit depth] Resaves the current image entry as an ome.tiff #qupath #groovy
// Resave image as a pyramidal ome.tiff at the desired downsample and bit depth
// @author Olivier Burri
// 16-bit magic trick inspired from
// https://forum.image.sc/t/software-recommendations-converting-a-32bit-ome-tiff-to-an-8bit-ome-tiff/43043
// But did not use scaling parameter
// ****** PARAMETERS *********
// Output downsample (1 for full-resolution)
def outputDownsample = 1
def bitDepth = PixelType.UINT16 // See https://qupath.github.io/javadoc/docs/qupath/lib/images/servers/PixelType.html
def tilesize = 512
def pyramidscaling = 2
def nThreads = 8
def compression = OMEPyramidWriter.CompressionType.ZLIB //ZLIB //UNCOMPRESSED //LZW (not working with 32bit: //J2K_LOSSY //J2K)
// ****** SCRIPT *********
def imageData = getCurrentImageData()
// Build downscaled and bit-depth clipped server
def op = ImageOps.buildImageDataOp().appendOps( ImageOps.Core.ensureType( bitDepth ) )
// Create bit-depth-clipping server
def server = ImageOps.buildServer( imageData, op, imageData.getServer().getPixelCalibration().createScaledInstance( outputDownsample, outputDownsample ) )
// Create output image path
def imageName = getCurrentImageName()
def savePath = buildFilePath( PROJECT_BASE_DIR, imageName + ".ome.tif" )
//Build the writer and run with all reviously set parameters
new OMEPyramidWriter.Builder( server )
.compression( compression )
.parallelize( nThreads )
.channelsInterleaved()
.tileSize( tilesize )
.scaledDownsampling( outputDownsample, pyramidscaling )
.build()
.writePyramid( savePath )
println( 'Done:' + savePath )
// Necessary imports
import qupath.lib.images.writers.ome.OMEPyramidWriter
import qupath.opencv.ops.ImageOps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment