def create_image(dicom_dataset, data_element): """Create a gdcm.Image from a FileDataset and a gdcm.DataElement containing PixelData (0x7fe0, 0x0010) Parameters ---------- dicom_dataset : FileDataset data_element : gdcm.DataElement DataElement containing PixelData Returns ------- gdcm.Image """ image = gdcm.Image() number_of_frames = getattr(dicom_dataset, 'NumberOfFrames', 1) image.SetNumberOfDimensions(2 if number_of_frames == 1 else 3) image.SetDimensions( (dicom_dataset.Columns, dicom_dataset.Rows, number_of_frames)) image.SetDataElement(data_element) pi_type = gdcm.PhotometricInterpretation.GetPIType( dicom_dataset.PhotometricInterpretation) image.SetPhotometricInterpretation(gdcm.PhotometricInterpretation(pi_type)) ts_type = gdcm.TransferSyntax.GetTSType( str.__str__(dicom_dataset.file_meta.TransferSyntaxUID)) image.SetTransferSyntax(gdcm.TransferSyntax(ts_type)) pixel_format = gdcm.PixelFormat(dicom_dataset.SamplesPerPixel, dicom_dataset.BitsAllocated, dicom_dataset.BitsStored, dicom_dataset.HighBit, dicom_dataset.PixelRepresentation) image.SetPixelFormat(pixel_format) if 'PlanarConfiguration' in dicom_dataset: image.SetPlanarConfiguration(dicom_dataset.PlanarConfiguration) return image
def create_image(ds: "Dataset", data_element: "DataElement") -> "gdcm.Image": """Return a ``gdcm.Image``. Parameters ---------- ds : dataset.Dataset The :class:`~pydicom.dataset.Dataset` containing the Image Pixel module. data_element : gdcm.DataElement The ``gdcm.DataElement`` *Pixel Data* element. Returns ------- gdcm.Image """ image = gdcm.Image() number_of_frames = getattr(ds, 'NumberOfFrames', 1) image.SetNumberOfDimensions(2 if number_of_frames == 1 else 3) image.SetDimensions((ds.Columns, ds.Rows, number_of_frames)) image.SetDataElement(data_element) pi_type = gdcm.PhotometricInterpretation.GetPIType( ds.PhotometricInterpretation) image.SetPhotometricInterpretation(gdcm.PhotometricInterpretation(pi_type)) ts_type = gdcm.TransferSyntax.GetTSType( str.__str__(ds.file_meta.TransferSyntaxUID)) image.SetTransferSyntax(gdcm.TransferSyntax(ts_type)) pixel_format = gdcm.PixelFormat(ds.SamplesPerPixel, ds.BitsAllocated, ds.BitsStored, ds.HighBit, ds.PixelRepresentation) image.SetPixelFormat(pixel_format) if 'PlanarConfiguration' in ds: image.SetPlanarConfiguration(ds.PlanarConfiguration) return image
ori3 = (0, 1, 0, 0, 0, 1) label1 = gdcm.Orientation.GetLabel(gdcm.Orientation.GetType(ori1)) if label1 != 'AXIAL': print("Found:", label1) sucess = False label2 = gdcm.Orientation.GetLabel(gdcm.Orientation.GetType(ori2)) if label2 != 'CORONAL': print("Found:", label2) sucess = False label3 = gdcm.Orientation.GetLabel(gdcm.Orientation.GetType(ori3)) if label3 != 'SAGITTAL': print("Found:", label3) sucess = False image = gdcm.Image() image.SetNumberOfDimensions(2) print(image) print(image.GetDimensions()) print(image.GetOrigin()) print(image.GetSpacing()) print(image.GetDirectionCosines()) image.SetNumberOfDimensions(3) image.SetDimensions((512, 256, 128)) print(image.GetDimensions()) #dircos = gdcm.DirectionCosines( (1,0,0,0,0,-1) ) dircos = gdcm.DirectionCosines() print(dircos) #print dircos.Cross()
def _create_gdcm_image(src: bytes, **kwargs: Any) -> "gdcm.Image": """Return a gdcm.Image from the `src`. Parameters ---------- src : bytes The raw image frame data to be encoded. **kwargs Required parameters: * `rows`: int * `columns`: int * `samples_per_pixel`: int * `number_of_frames`: int * `bits_allocated`: int * `bits_stored`: int * `pixel_representation`: int * `photometric_interpretation`: str Returns ------- gdcm.Image An Image containing the `src` as a single uncompressed frame. """ rows = kwargs['rows'] columns = kwargs['columns'] samples_per_pixel = kwargs['samples_per_pixel'] number_of_frames = kwargs['number_of_frames'] pixel_representation = kwargs['pixel_representation'] bits_allocated = kwargs['bits_allocated'] bits_stored = kwargs['bits_stored'] photometric_interpretation = kwargs['photometric_interpretation'] pi = gdcm.PhotometricInterpretation.GetPIType(photometric_interpretation) # GDCM's null photometric interpretation gets used for invalid values if pi == gdcm.PhotometricInterpretation.PI_END: raise ValueError( "An error occurred with the 'gdcm' plugin: invalid photometric " f"interpretation '{photometric_interpretation}'") # `src` uses little-endian byte ordering ts = gdcm.TransferSyntax.ImplicitVRLittleEndian image = gdcm.Image() image.SetNumberOfDimensions(2) image.SetDimensions((columns, rows, 1)) image.SetPhotometricInterpretation(gdcm.PhotometricInterpretation(pi)) image.SetTransferSyntax(gdcm.TransferSyntax(ts)) pixel_format = gdcm.PixelFormat(samples_per_pixel, bits_allocated, bits_stored, bits_stored - 1, pixel_representation) image.SetPixelFormat(pixel_format) if samples_per_pixel > 1: # Default `src` is planar configuration 0 (i.e. R1 G1 B1 R2 G2 B2) image.SetPlanarConfiguration(0) # Add the Pixel Data element and set the value to `src` elem = gdcm.DataElement(gdcm.Tag(0x7FE0, 0x0010)) elem.SetByteStringValue(src) image.SetDataElement(elem) return cast("gdcm.Image", image)