How do I write scripts?#
What will I learn?
What is a script?
How to find existing scripts
How to create a basic script from scratch
The user interface is the most user-friendly way of working with QuPath. However, it has some limits:
Analysis is done by hand and takes some time.
It doesn’t allow processing similar images in batch mode.
It doesn’t encourage reproducibility in analysis.
QuPath supports scripts as an alternative.
Scripts represent sequences of instructions that are interpreted by QuPath, for example:
Setting the image type.
Setting the colour deconvolution stains.
Applying a cell detection algorithm.
In QuPath, scripts are written in the script editor. You can open it by clicking on .
Fig. 91 The QuPath script editor#
Groovy#
Scripts are written in Groovy. This is a language similar to Python but for the Java platform.
We created a script containg an overview of the Groovy syntax. This can be useful to have when beginning with Groovy scripting.
Finding existing scripts#
Before writing your own scripts, you can check if the script you want to write already exists. The user forum is a good source for scripts.
Be aware that scripts found on the Internet can be outdated: a script written for an earlier version is not guaranteed to work for the latest version.
What to do
Create an empty folder on your computer.
Open the
LuCa-7color_[13860,52919]_1x1component_data.tifimage in QuPath.Create a few rectangle annotations.
Go to this image.sc topic.
The post contains a script that exports all parts of the image located below annotations. Copy the script.
Go to QuPath and click on . This opens up the script editor.
Paste the script.
Change
/path/to/some/folder/to the path of the folder you created on step 1. On Windows, the path should look likeC:\Users\User\someFolder. On MacOS, the path should look like/Users/user/someFolder.Click on
Runin the bottom right corner of the script editor. After some time,Doneshould be printed at the bottom of the script editor.Check the folder you created on step 1. It should now contain images named
1.ome.tiff,2.ome.tiff, and so on.Drag
1.ome.tiffto QuPath to check the first image. It should represent a subset of the original image.
Creating basic scripts#
QuPath has some functions dedicated to scripting in two classes:
These functions should be enough for common use cases.
What to do
Go to QP.
Find a function that returns the name of the current image. You can use Control/Command+F to search for the function.
Open any image in QuPath.
Open the script editor.
Use the function of step 2 to print the name of the current image.
Hint: you can use:
println someFunction()
to print the result of someFunction().
Tip
ChatGPT is not good at generating QuPath scripts.
Asking help#
You can use image.sc if you need help regarding a script (or anything else related to QuPath).
If you embed a script in your post, please surround your code with ```. This adds special formatting that makes it easier for others to copy the script.