pythondicompydicomgdcm

"TypeError: a bytes-like object is required, not 'str'" reading compressed DICOM volume into numpy array


I am using Python 3.7.3 on Anaconda Spyder on CentOS 7.

I have a 3D DICOM volume that is in a single file:/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm

print(image.file_meta.TransferSyntaxUID)

returns

1.2.840.10008.1.2.4.70


import pydicom as dicom
image=dicom.read_file('/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm')
image.pixel_array
image.BitsStored

returns 12.

I try to read the volume with the following code.

import numpy as np
import gdcm

def get_numpy_array_type(gdcm_pixel_format):
    """Returns a numpy array typecode given a GDCM Pixel Format."""
    return get_gdcm_to_numpy_typemap()[gdcm_pixel_format]

def get_gdcm_to_numpy_typemap():
    """Returns the GDCM Pixel Format to numpy array type mapping."""
    _gdcm_np = {gdcm.PixelFormat.UINT8  :np.int8,
                gdcm.PixelFormat.INT8   :np.uint8,
                gdcm.PixelFormat.UINT16 :np.uint16,
                gdcm.PixelFormat.INT16  :np.int16,
                gdcm.PixelFormat.UINT32 :np.uint32,
                gdcm.PixelFormat.INT32  :np.int32,
                gdcm.PixelFormat.FLOAT32:np.float32,
                gdcm.PixelFormat.FLOAT64:np.float64 }
    return _gdcm_np

r = gdcm.ImageReader()
r.SetFileName('/usr/share/aliza/datasets/DICOM/00_MR/Tra_FLAIR.dcm')
if not r.Read():
    print('Error reading input')

image = gdcm.Image()
ir = r.GetImage()
dims = ir.GetDimensions()

image.SetDimension(0, ir.GetDimension(0) );
image.SetDimension(1, ir.GetDimension(1) );
image.SetDimension(2, ir.GetDimension(2) );

pixeltype = ir.GetPixelFormat();
image.SetPixelFormat( pixeltype );

pi = ir.GetPhotometricInterpretation();
image.SetPhotometricInterpretation( pi );

pixeldata = gdcm.DataElement( gdcm.Tag(0x7fe0,0x0010) )
str1 = ir.GetBuffer()

image.SetDataElement( pixeldata )
pf = image.GetPixelFormat()
dtype = get_numpy_array_type(pf.GetScalarType())
gdcm_array = image.GetBuffer()
result = np.frombuffer(gdcm_array, dtype=dtype)

This results in

TypeError: a bytes-like object is required, not 'str'

Solution

  • GDCM's Image.GetBuffer() returns a char* type as a Python str, so for Python 3 it needs to be encoded back to bytes as stated here:

    gdcm_array = image.GetBuffer().encode("utf-8", "surrogateescape")