Re: Script(s) of the Day: Exporting & importing binary masks

Hi all, I am new to QuPath and Groovy scripting.
For this reason I am using all the amazing pre-written scripts from Pete and everyone else on here. So thank you.

I am using this script currently:

And it worked brilliantly, I also managed to edit it thanks to a comment from Pete it to export the images full size.

I wanted to add some lines so if there were no annotations it would export the entire bright field image anyway and a matched sized black square for the mask. I got a bit muddled where to add this and what I would need to add.

Sorry I can’t add some example code, if its easier I can give it a go.

Thanks again for any help

Hi @HayleyJeanPye ,

If you want, when there is no annotation, you can create a full image annotation and run the same steps:

if (getAnnotationObjects().size() == 0)

// The rest of your script...
1 Like

Thank you so much ! Such an elegant answer. Really appreciate it, this community is great, hopefully I can help others in future (just starting out now).

Just tried it and it works really well apart from the fact exported png is all white not all black, because it thinks the image is one large annotation. Hopefully I can work out how to change that - and they I will have it perfect.

Update - I am taking these images forward into Image J / FIJI so just added in some code to invert the image there if there were no black pixels! Thanks again.

1 Like

I just want to point out that the unclassified regions should still be “0”, but you can control if they appear black or white.
The backgroundLabel in this case is any unclassified annotation, like what @melvingelbard included.

1 Like