Upload DICOM series from local directory to Supervisely platform.
Source code:
for serie_id, files in series_infos.items():
item_path = files[0]
name = f"{sly.fs.get_file_name(path=item_path)}.nrrd"
dicom_info = api.volume.upload_dicom_serie_paths(
dataset_id=dataset.id,
name=name,
paths=files,
anonymize=True,
)
print(f"DICOM volume has been uploaded to Supervisely with ID: {dicom_info.id}")
Set anonymize=True if you want to anonymize DICOM series and hide PatientID and PatientName fields.
Output:
# DICOM volume has been uploaded to Supervisely with ID: 18630608
Upload list of volumes from local directory
Source code:
local_dir_name = "src/upload/nrrd/"
all_nrrd_names = os.listdir(local_dir_name)
names = [f"1_{name}" for name in all_nrrd_names]
paths = [os.path.join(local_dir_name, name) for name in all_nrrd_names]
volume_infos = api.volume.upload_nrrd_series_paths(dataset.id, names, paths)
print(f"All volumes has been uploaded with IDs: {[x.id for x in volume_infos]}")
Output:
# All volumes has been uploaded with IDs: [18630605, 18630606, 18630607]
Get slices from current volume. In this example we will get sagittal slices.
Source code:
slices = {}
dimension = volume_np.shape[0] # change index: 0 - sagittal, 1 - coronal, 2 - axial
for batch in sly.batched(list(range(dimension))):
for i in batch:
if i >= dimension:
continue
pixel_data = volume_np[i, :, :] # sagittal
# pixel_data = volume_np[:, i, :] # coronal
# pixel_data = volume_np[:, :, i] # axial
slices[i] = pixel_data
print(f"{len(slices.keys())} slices has been received from current volume.")
Output:
# 130 slices has been received from current volume.
✅ There is a built-in function supervisely.image.write which reads file extension from path and saves image (slice) with the desired format in local directory.
Example:
import supervisely as sly
sly.image.write("folder/slice.nrrd", image_np) # save as NRRD
sly.image.write("folder/slice.jpg", image_np) # save as JPG
Save slice as NRRD
Recommended way to save slice as NRRD file to preserve image quality (pixel depth)
Source code:
# save slice as NRRD file
save_dir = "src/download/"
nrrd_slice_path = os.path.join(save_dir, 'slice.nrrd')
sly.image.write(nrrd_slice_path, image_np)
Save slice as JPG
Source code:
# save slice as jpg
save_dir = "src/download/"
image_slice_path = os.path.join(save_dir, 'slice.jpg')
sly.image.write(jpg_slice_path, image_np)
Note:
In case you save slice using nrrd library, it is recommended to use C-order indexing.