import static qupath.lib.roi.PathROIToolsAwt.getShape;
import java.awt.image.BufferedImage
import java.awt.Color
import javax.imageio.ImageIO
// Get java.awt.Shape objects for each annotation
def shapes = getAnnotationObjects().collect({getShape(it.getROI())})
// Create a grayscale image, here it's 10% of the full image size
double downsample = 10.0
def server = getCurrentImageData().getServer()
int w = (server.getWidth() / downsample) as int
int h = (server.getHeight() / downsample) as int
def img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY)
// Paint the shapes (this is just 'standard' Java - you might want to modify)
def g2d = img.createGraphics()
g2d.scale(1.0/downsample, 1.0/downsample)
g2d.setColor(Color.WHITE)
for (shape in shapes)
g2d.fill(shape)
g2d.dispose()
// Save the result
def outputFile = getQuPath().getDialogHelper().promptToSaveFile("Save binary image", null, null, "PNG", ".png")
ImageIO.write(img, 'PNG', outputFile)
There isn't very much that is QuPath-specific in there - just a few key lines to extract the parts that you need, and then it turns into a Java problem. For maximum accuracy, you might want to experiment a bit with the painting (e.g. shifting things by half a pixel, or drawing outlines of the shapes in addition to filling them) - in case the default doesn't quite interpret coordinates in the way that you want.