Ha, I had absolutely no memory whatsoever of creating a
loadImageData method for scripting. Intriguing... if only there were also the other helper methods to make it more useful.
Anyhow, this should do what you need (and a little extra):
import qupath.lib.gui.QuPathGUI
// Get a list of all images in the project, except the current one
def project = getProject()
def entries = project.getImageList()
entries.remove(getProjectEntry())
// Get path to a .qpdata file
def file = QuPathGUI.getImageDataFile(project, entries[0])
// Read the data
def imageData = loadImageData(file.getAbsolutePath(), false)
print imageData
The key thing is that you need the path to the .qpdata file, not to the image. The easiest way to get that is to call a static method in the GUI class... which is admittedly a little strange.
Anyhow, while forgetting the existence of loadImageData the other way to do it more directly is this:
import java.awt.Image.BufferedImage
import qupath.lib.io.PathIO
imageData = PathIO.readImageData(file, null, null, BufferedImage.class)
The nulls are a bit strange. If you care why they exist,
this is the place to look. Basically, if you pass only the .qpdata file path then QuPath will read the image path from that... but the image path is also stored in the project
and might be updated there (because it's comparatively easy to update the paths in the project, since it's just text/JSON - and necessary if images are moved). So the ability to use a pre-existing
ImageServer object means that the
ImageData can be created without depending on the .qpdata file containing the right path.
One last thing: if you don't care about the pixels at all at this point, only annotations, you can use
import qupath.lib.io.PathIO
hierarchy = PathIO.readHierarchy(file)
This way you don't need to bother with the image server at all. It should also be much faster, because constructing the server is the slowest part... although that may be less important in a project with two images.