Exporting Multiple Images

I am trying to export images for each tumor core in my IF project with and without annotation/detection overlay. I know you can export images manually from the viewer however this is quite tedious. Does anyone have any scripts that can be used to export images?

I don’t know of anything offhand for m2, exactly, but you might be able to adapt some of the other scripts to do that.



Probably a few other posts on the topic here and in the Issues.

I’ve done a lot of looking around and this is the closest script I can find, however it doesn’t seem to work with m-2 (the script was written by Pete 3 years ago). I edited the script slightly because it was having trouble with the QPEx extension however it still pops up with an error claiming it is unable to write the file path followed by “No compatible writer found”.

Does anyone know what the problem is with the script here? I’m not familiar with the ImageWriterTools.

/**
 * Export a thumbnail image, with and without an overlay, using QuPath.
 *
 * For tissue microarrays, the scripting code written by the 'File -> Export TMA data'
 * command is probably more appropriate.
 *
 * However, for all other kinds of images where batch export is needed this script can be used.
 *
 * @author Pete Bankhead
 */


import qupath.lib.gui.ImageWriterTools
import qupath.lib.gui.QuPathGUI
import qupath.lib.gui.viewer.OverlayOptions
import qupath.lib.regions.RegionRequest

// Aim for an output resolution of approx 20 µm/pixel
double requestedPixelSize = 20

// Create the output directory, if required
def path = buildFilePath(PROJECT_BASE_DIR, "image_exports")
mkdirs(path)

// Get the imageData & server
def imageData = getCurrentImageData()
def server = imageData.getServer()

// Get the file name from the current server
def name = server.getShortServerName()

// We need to get the display settings (colors, line thicknesses, opacity etc.) from the current viewer, if available
def overlayOptions = QuPathGUI.getInstance() == null ? new OverlayOptions() : QuPathGUI.getInstance().getViewer().getOverlayOptions()

// Calculate downsample factor depending on the requested pixel size
double downsample = requestedPixelSize / server.getAveragedPixelSizeMicrons()
def request = RegionRequest.createInstance(imageData.getServerPath(), downsample, 0, 0, server.getWidth(), server.getHeight())

// Write output image, with and without overlay
def dir = new File(path)
def fileImage = new File(dir, name + ".jpg")
def img = ImageWriterTools.writeImageRegion(server, request, fileImage.getAbsolutePath())
def fileImageWithOverlay = new File(dir, name + "-overlay.jpg")
ImageWriterTools.writeImageRegionWithOverlay(img, imageData, overlayOptions, request, fileImageWithOverlay.getAbsolutePath())

Also here is the link to the original post by Pete:

Because you mention IF, I assume you don’t have an 8-bit RGB image - which is why it can’t be written to a JPEG. If you change the extension to ‘.tif’ it may work better (I’m not certain)… but it really depends what you want: the raw pixel values, or a RGB snapshot for display only. The method of export would be different in each case.

2 Likes

Somewhere in… I think Issues, though it may be the old forum (waaay back), there was a post where someone was trying to use an image writer that only worked with brightfield images on an IF image.

*Ninjaed

Changing the extension to .tif helped. The script now prints 2 image: one with overlay and another without overlay. Unfortunately the image without overlay does not show the different channels properly (it’s all black and white) however I can get around this by running the script a second time with annotation/detection display turned off.

Thanks for your help!