Skip to content

Instantly share code, notes, and snippets.

View petebankhead's full-sized avatar

Pete petebankhead

View GitHub Profile
@petebankhead
petebankhead / QuPath-Open dialogs on startup.groovy
Last active October 17, 2024 14:12
Example QuPath startup script showing how to open specific dialogs
/**
* Example QuPath startup script to open the brightness/contrast dialog & channel viewer,
* optionally setting their location.
*
* Originally posted at https://forum.image.sc/t/saving-window-layout/103763/3
*
* Written for QuPath v0.5.x
*/
@petebankhead
petebankhead / QuPath-Get window location.groovy
Created October 16, 2024 04:42
Quick Groovy script to get the location of a window in a JavaFX application
/**
* Quick Groovy script to get the location of a window in a JavaFX application.
* Originally posted at https://forum.image.sc/t/saving-window-layout/103763/3
* Written for QuPath v0.5.x
*/
import javafx.stage.Window
String title = "Brightness & contrast"
@petebankhead
petebankhead / QuPath-Convert images for web.groovy
Created October 10, 2024 16:53
QuPath script to help convert one or more images to PNG or JPEG, while also reducing the dimensions and file size
/**
* QuPath script to help convert one or more images to PNG or JPEG,
* while also reducing the dimensions and file size.
*
* The purpose is to help convert images for websites in a way that balances file size with quality.
*
* Given an image file or directory containing multiple files (usually screenshots), this will:
* - Strip away any unnecessary alpha channel
* - Resize so that the width and height are <= a specified maximum dimension
* - Iteratively try to convert the image to PNG and/or JPEG by
@petebankhead
petebankhead / QuPath-Make project self-contained.groovy
Created October 3, 2024 05:39
QuPath script to copy simple, single-file images into a project to make the project self-contained.
/**
* QuPath script to copy images into a project.
* This is used to help make the project directory self-contained.
*
* By default, local files are copied into an 'images' subdirectory of the project.
*
* Limitation: This only handles local files that are currently outside the project directory,
* and excludes images that are known to have 'multipart' files (e.g. dicom, mrxs, vsi & zarr).
*
* Warning: This has not been very extensively tested!
@petebankhead
petebankhead / QuPath-Concentric rings.groovy
Created September 4, 2024 16:44
QuPath-Create concentric rings around an annotation
/**
* QuPath script to create concentric rings around an annotation.
*/
// Number of rings to create
int nRings = 5
// Define distance in µm, then convert (or just go straight to defining in pixels)
double distanceMicrons = 100
double distancePixels = distanceMicrons / getCurrentServer().getPixelCalibration().getAveragedPixelSizeMicrons()
@petebankhead
petebankhead / QuPath-Polylines to polygons.groovy
Created May 1, 2024 07:59
Script to convert all polyline annotations to polygons in QuPath.
/**
* Script to convert all polyline annotations to polygons in QuPath.
* Written for QuPath v0.5 (may work in other versions).
*/
def lineObjects = getAnnotationObjects().findAll {it.getROI().isLine()}
def polygonObjects = []
def selectedObjects = getSelectedObjects() as List
for (lineObject in lineObjects) {
def line = lineObject.getROI()
@petebankhead
petebankhead / Create PyTorch launcher (Windows).groovy
Last active November 16, 2023 15:28
QuPath script to create a .bat file to launch QuPath using paths from a conda environment
/**
* Groovy script to generate a batch script to launch QuPath while setting the PATH
* from a conda environment.
*
* This is useful to add GPU support with Deep Java Library (DJL) without needing to install
* CUDA system-wide.
*
* This script was written for QuPath v0.5.
* You should read the top part and change the variables to match what you need.
*/
@petebankhead
petebankhead / QuPath-Threshold pixel classification.groovy
Created June 14, 2023 08:58
QuPath script to threshold pixel classification outputs with fixed (non-default) threshold values
/**
* QuPath v0.4.3 script to threshold probability outputs from a pixel classifier.
*
* By default, converting the pixel classifier output to probabilities will always
* apply a softmax operation and take the class with the highest probability.
*
* This script provides an alternative, whereby you can specify a probability threshold
* for one or more channels, and threshold to create objects from that.
* Using a higher threshold can then restrict the object creation of more confident predictions.
*
@petebankhead
petebankhead / QuPath-Export images for annotation bounding boxes.groovy
Created May 29, 2023 12:06
QuPath script to export images corresponding to all the annotation bounding boxes in an image
/**
* QuPath script to export images corresponding to all the annotation bounding boxes in an image.
* Written for QuPath v0.4.3.
*/
// Export at full resolution (or change this value)
double downsample = 1.0
// Export to a subdirectory of the current project
def dir = buildPathInProject("export")
@petebankhead
petebankhead / QuPath-Create surrounding annotation.groovy
Last active May 11, 2023 16:52
Interactively create a convex or concave hull annotation from the selected objects in QuPath v0.4.
/**
* Create a convex or concave hull annotation from the selected objects.
*
* The calculations are performed using Java Topology Suite's 'ConcaveHullOfPolygons' class -
* see https://locationtech.github.io/jts/javadoc/org/locationtech/jts/algorithm/hull/ConcaveHullOfPolygons.html
*
* This was written for QuPath v0.4.3.
* If it's useful enough, a similar feature might be added to QuPath directly in the future.
*
* @author Pete Bankhead