Module 1: Introduction to Basic Scripting

Learning Objectives

  • Understand the fundamentals of Groovy scripting in QuPath.
  • Access and manipulate image metadata programmatically.
  • Split image channels to isolate different stains.
  • Export full images with downsampling for manageable file sizes.
  • Export annotated patches to focus on regions of interest.
  • Consult documentation (Javadoc) for troubleshooting errors.

Key Concepts

Use the arrows below to learn about theoretical concepts behind QuPath scripting:

Loading...
Please wait while content loads...

Using QuPath Desktop

Use the arrows below to view code examples for each concept:

Loading...
Please wait while content loads...

Practice Exercise

Quick Quiz

1. Which of the following correctly accesses the current image in a QuPath script?

2. What is the purpose of downsampling when exporting images?

3. How would you access the server from the current image data?

4. Which class is used to export tiles from an image?

5. What resource should you consult for detailed information about QuPath classes and methods?

Cheat Sheet

Common QuPath scripting commands reference:

// Accessing the current image and its components
def imageData = getCurrentImageData()
def server = imageData.getServer()
def viewer = getCurrentViewer()
def hierarchy = imageData.getHierarchy()

// Working with metadata
def pixelCalibration = server.getPixelCalibration()
def pixelWidthMicrons = pixelCalibration.getPixelWidth().doubleValue()
def magnification = server.getMetadata().getMagnification()

// Exporting images
def outputPath = buildFilePath(PROJECT_BASE_DIR, "exported_image.tif")
def downsample = 4.0 // Use 4x downsampling
def request = RegionRequest.createInstance(server, downsample)
writeImageRegion(server, request, outputPath)

// Working with annotations
def annotations = getAnnotationObjects()
def selected = getSelectedObject()

// Creating a tile exporter
def exporter = new TileExporter(imageData)
    .downsample(4.0)
    .imageExtension('.jpg')
    .tileSize(512)
    .annotatedTilesOnly(true)
    .writeTiles(outputPath)

// File paths and directories
def path = buildFilePath(PROJECT_BASE_DIR, "subfolder", "file.tif")
mkdirs(buildFilePath(PROJECT_BASE_DIR, "subfolder"))

For more details, visit the official QuPath Javadoc.