If you are looking for a function that is not on this page, a great place to look is the QPEx.java file, which can be found here, on GitHub.
New as of version 0.3.0, API docs show most of the main scripting methods, as another quick reference list.

  • General

 
describe(aThing)  //to find functions that can be done with/to this object

//Get the name of the current image without its extension. Great for naming export files
name = GeneralTools.getNameWithoutExtension(imageData.getServer().getMetadata().getName())

//Other methods to get image names - with extension
nameOfImageInViewer = getCurrentViewer().getServer().getFile().getName()
nameOfActiveImage = getProjectEntry().getImageName()

getCurrentHierarchy()
getCurrentViewer()
getCurrentImageData()
getCurrentServer()

//Combine describe and the getCurrent functions to get a better idea of what each represents

//Find the current average pixel size of the image - assumes square sized pixels which is not always the case
double pixelSize = imageData.getServer().getPixelCalibration().getAveragedPixelSize()

//Other useful pixel functions via getPixelCalibration() include
.getAveragedPixelSizeMicrons()
.getPixelHeight()
.getPixelWidth()
.getPixelWidthUnit()
//find even more using describe()
  •  Selecting

 
resetSelection() selectAnnotations() selectObjectsByClassification("Necrosis", "Tumor"); selectDetections() selectCells() selectAllObjects() selectTMACores()
  • Selecting subsets

In setSelectedObject, true indicates the object or list should be added to currently selected objects, while false will select ONLY the object indicated, unselecting everything else.

 
getCurrentHierarchy().getSelectionModel().setSelectedObject(objectToSelect, true); getCurrentHierarchy().getSelectionModel().selectObjects(listToSelect);
  • Print data labels (great for getting micron symbol, less necessary in 0.3.0 with the ability to copy out of the key/value pairings)

 
PathClassifierTools.getAvailableFeatures(getDetectionObjects()).each { println(it) }
  • Deleting

 
removeObject(object, true) clearSelectedObjects(); removeObjects(listOfObjects,true)
  • Creating

 
addObject(object) addObjects(listOfObjects) createSelectAllObject(true)
  • Hierarchy commands (Ok, not all of these are simple, but still it should be a useful reference)

 
resolveHierarchy() //try to naturally resolve all objects

//Let the interface know when something has changed, sometimes reclassifications by script will only show up when you zoom in and out, or similar
fireHierarchyUpdate()

//All together:
import qupath.lib.gui.commands.Commands
imageData = getCurrentImageData();
//Select something, see selection macros above, then
Commands.insertSelectedObjectsInHierarchy(imageData)

//Alternatively
someHierarchy.insertPathObjects(collectionOfObjects);
//or
insertObjects(collectionOfObjects)


//Another option for a new set of objects in the variable outsideCells, for example
getCurrentHierarchy().insertPathObjects(outsideCells)


//Add some objects to a particular hierarchy, maybe in another image
otherHierarchy.addPathObjects(objects) //allows you to move objects between images

//Adding detections below other detections - REINSERT SUBCELLULAR DETECTION!!!
getCellObjects().each{

    subcells = getCurrentHierarchy().getObjectsForROI(qupath.lib.objects.PathDetectionObject, it.getROI()).findAll{s->!s.isCell()}
    it.addPathObjects(subcells);
}
fireHierarchyUpdate()
  • Converting data types

 
notAString.toString() string = "50" double number = string as double //"number" is now 50.0, a double
  • Measurements

 
object.measurements.put("measurement name", doubleValue)
double cellArea = measurement(cell, "Cell: Area")
cellAreaPixels = cell.getROI().getArea()
nuclearAreaPixels = cell.getNucleusROI().getArea()
//Multiply the above value by the pixel area (pixel height*pixel width) to get the area in microns)
  • Object commands

 
String name = object.getName() object.setName("SomeString") def roi = object.getROI() object.setPathClass(getPathClass("Tumor"))
  • Collecting objects into a variable

 
detections = getDetectionObjects()
annotations = getAnnotationObjects()
cells = getCellObjects()
// example of list.collect{some code that returns something}
listOfAreasFromEachCell = cells.collect{measurement(it, "Nucleus: Area")}
//Returning a single object
selected = getSelectedObject()
//Return children of a selected object - based on hierarchy
children = getSelectedObject().getChildObjects()
//Return all selected objects - this will return a list even if it is a list of 1 object!
selected = getSelectedObjects()

//get ALL objects, including the IMAGE ITSELF
everythingAndTheImage=getAllObjects()
  • Collect objects based on placement in the image

 
//all Detections within another object’s ROI (note the getROI() at the end) objectsInside = getCurrentHierarchy().getObjectsForROI(qupath.lib.objects.PathDetectionObject, objectIWantThingsInsideOf.getROI()) //All child objects of a single selected annotation (in this case) - what you get here can be tricky depending on whether resolveHierarchy() has been run or not childObjects = getSelectedObject().getChildObjects()
 

Slightly more complex

  • Manual creation of an object, add it to a list, then create the list

 
def roi = new RectangleROI(x,y,size,size, ImagePlane.getDefaultPlane()) newTiles << PathObjects.createDetectionObject(roi, getPathClass(c)) // //Some time later addObjects(newTiles) //Saving data in an image getProject().getEntry(getCurrentImageData()).saveImageData(getCurrentImageData()) //Use imageData from a different image entry = getProject().getImageList().find{it.getImageName() == "CMU-1.svs"} entryImageData = entry.readImageData() //do some things to the hierarchy of that image //e.g. entryImageData().getHierarchy().getAnnotationObjects() //more code entry.saveImageData(entryImageData)

Access the direct path to an image

 
def server = getCurrentServer() def uris = server.getURIs() def file = new File(uris[0]) println file.getAbsolutePath()

Classify specific objects (subsets of cells)

 
def classifier = loadObjectClassifier(name) classifier.classifyObjects(imageData, cells, false)

An outline of how code objects in QuPath relate to one another.

An outline of how code objects in QuPath relate to one another.