Skip to content
Snippets Groups Projects

Load dicom

3 files
+ 33
1
Compare changes
  • Side-by-side
  • Inline

Files

+ 30
0
@@ -27,6 +27,7 @@ import h5py
import nibabel as nib
import numpy as np
import PIL
import pydicom
import tifffile
from qim3d.io.logger import log
@@ -179,6 +180,33 @@ class DataSaver:
with h5py.File(path, "w") as f:
f.create_dataset("dataset", data=data, compression="gzip" if self.compression else None)
def save_dicom(self, path, data):
""" Save data to a DICOM file to the given path.
Args:
path (str): The path to save file to
data (numpy.ndarray): The data to be saved
"""
ds = pydicom.Dataset()
# Set required DICOM attributes
ds.PatientName = "Anonymous"
ds.PatientID = "123456"
ds.BitsAllocated = 16
ds.BitsStored = 16
ds.HighBit = 15
# Convert the numpy array to bytes
pixel_data = data.astype('uint16').tobytes()
# Set the pixel data attribute
ds.PixelData = pixel_data
# Save the DICOM dataset to file
ds.save_as(path)
def save_PIL(self, path, data):
""" Save data to a PIL file to the given path.
@@ -271,6 +299,8 @@ class DataSaver:
return self.save_h5(path, data)
elif path.endswith((".vol",".vgi")):
return self.save_vol(path, data)
elif path.endswith((".dcm",".DCM")):
raise self.save_dicom(path, data)
elif path.endswith((".jpeg",".jpg", ".png")):
return self.save_PIL(path, data)
else:
Loading