< Back to QuPath Article List

Intro to QuPath Scripting

I am Mike - but enough about me, this section of the webpage is all about QuPath, authored by Pete Bankhead. The official documentation can be found here:

QuPath Read the Docs

...so trust that as the best and most up-to-date source for all of your scripting needs. I will try to keep this section as up to date as possible. How successful I am will probably depend on how busy I am in grad school.

Other sources for scripting information have already been listed here, so consider some of those sources as well for anything I do not cover here. And, as always, take your questions to the image.sc forum where others can learn from your questions - and possibly find answers already waiting for you. Most of these scripts, lines of code, etc. came originally from Pete Bankhead or other sources on the forum, I am simply collecting them all in one place for educational purposes, not laying claim to any of them.

The goal for these pages is to serve as an introduction to scripting in QuPath, not an introduction to scripting or coding in general. I will do my best to explain most of the details I think a user would need to get started, but I also do not want the site to become too unfocused by covering general coding issues. If I can clarify anything, please ask on the forum - and if I get enough questions about something, I will probably fill in additional information. If you run into an error within QuPath, you may be able to provide more information by going through View->Show Log

For the moment, within the guide I will mostly be referencing a test project I will keep in this folder, consisting of a couple of demo images that are linked to in the QuPath documentation:

Images used in the test project

CMU-1.svs

LuCa-7color

When using the demo project:
To load test data for a given example, go to the Automate->Project Scripts-> # Name of folder and use one of the LOAD OBJECTS scripts for the appropriate image. You can reuse the script to reset the objects in the image. The name of the image the script is for should appear as the first word in the script name.

Also, I am not really a coder (I do not currently use an IDE, nor have I built any of my own extensions), just an extensive scripter - and it will probably show. There are many better programmers than I on the forum if you have more technical questions. I make frequent use of my own quick reference guide! If you have Java experience, but are new to Groovy, you may benefit from this differences guide.

Before Coding

The Run menu within the scripting dialog has some interesting features - it is good to know how they work.

symbol Info

Run - Runs the currently open script on the current image - the results ARE NOT SAVED. QuPath is generally working off of a temporary file, meaning anything you draw onto the image or create is not saved until you save it. That is an important distinction, because some functions like "Export measurements" work off of the saved files and will not reflect the current state of the current image.

Run for project - Runs the current script for each image in the project, and the results are saved TO THE DATA FILE. That means they are not saved to the currently open image. If you Run for Project only the current image, you should see no change (as the red warning text indicates). The script changes are applied to the already saved file - not the temporary one you are viewing. Reload the data from the File menu to see the changes.

Include default imports - Always have this checked, as far as I know. There are several forum issues when people accidentally uncheck this.

Log script time - I find this handy when checking how long a script took to run. Is it worth my time to optimize the speed of the script? How often will it be used?

Auto-clear console - Disable this if you want to keep errors or results around from previous runs of a script.