From 071e3c02c7c6d8c825d4a4dc2c4ab997321ebccc Mon Sep 17 00:00:00 2001 From: Kabilar Gunalan Date: Wed, 9 Oct 2024 16:00:56 -0500 Subject: [PATCH 1/2] Add example script to export webknossos volume annotations --- scripts/export_annotation.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scripts/export_annotation.py diff --git a/scripts/export_annotation.py b/scripts/export_annotation.py new file mode 100644 index 00000000..bb6c20a7 --- /dev/null +++ b/scripts/export_annotation.py @@ -0,0 +1,35 @@ +import os +import numpy as np +import webknossos as wk +from tifffile import imwrite + +ANNOTATION_ID = '' +SEGMENT_IDS = [1,2] +MAG = wk.Mag('8-8-1') +annotation_directory='~/example_export' + +with wk.client.context.webknossos_context(url=os.environ.get('WK_URL'),token=os.environ.get('WK_TOKEN')): + dataset = wk.Annotation.open_as_remote_dataset( + ANNOTATION_ID, + annotation_type='Volume', + webknossos_url=os.environ.get('WK_URL') + ) + mag_view = dataset.get_segmentation_layers()[0].get_mag(MAG) + +z = mag_view.bounding_box.topleft.z +with mag_view.get_buffered_slice_reader() as reader: + for slice_data in reader: + print(slice_data) + slice_data = slice_data[0] # First channel only + for segment_id in SEGMENT_IDS: + segment_mask = (slice_data == segment_id).astype( + np.uint8 + ) * 255 # Make a binary mask 0=empty, 255=segment + segment_mask = segment_mask.T # Tiff likes the data transposed + imwrite( + f"{annotation_directory}/seg{segment_id:04d}_mag{MAG}_z{z:04d}.tiff", + segment_mask, + ) + + print(f'Downloaded z={z:04d}') + z += MAG.z \ No newline at end of file From 453a60ecb42bafff1960bc8b53feea006adb5347 Mon Sep 17 00:00:00 2001 From: Kabilar Gunalan Date: Wed, 9 Oct 2024 16:07:32 -0500 Subject: [PATCH 2/2] Remove print statement --- scripts/export_annotation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/export_annotation.py b/scripts/export_annotation.py index bb6c20a7..e0666837 100644 --- a/scripts/export_annotation.py +++ b/scripts/export_annotation.py @@ -19,7 +19,6 @@ z = mag_view.bounding_box.topleft.z with mag_view.get_buffered_slice_reader() as reader: for slice_data in reader: - print(slice_data) slice_data = slice_data[0] # First channel only for segment_id in SEGMENT_IDS: segment_mask = (slice_data == segment_id).astype(