-
Notifications
You must be signed in to change notification settings - Fork 9
Description
I downloaded the example ConvertMPL.py from the example link in the readme. This example appears to be Python 2 code so I first converted to Python 3 with these changes:
--- a/dicom/ConvertMPL.py
+++ b/dicom/ConvertMPL.py
@@ -52,14 +52,14 @@ def gdcm_to_numpy(image):
"""Converts a GDCM image to a numpy array.
"""
pf = image.GetPixelFormat().GetScalarType()
- print 'pf', pf
- print image.GetPixelFormat().GetScalarTypeAsString()
+ print('pf', pf)
+ print(image.GetPixelFormat().GetScalarTypeAsString())
assert pf in get_gdcm_to_numpy_typemap().keys(), \
"Unsupported array type %s"%pf
d = image.GetDimension(0), image.GetDimension(1)
- print 'Image Size: %d x %d' % (d[0], d[1])
+ print('Image Size: %d x %d' % (d[0], d[1]))
dtype = get_numpy_array_type(pf)
- gdcm_array = image.GetBuffer()
+ gdcm_array = image.GetBuffer().encode("latin-1")
## use float for accurate scaling
result = numpy.frombuffer(gdcm_array, dtype=dtype).astype(float)
## optional gamma scaling
The print() changes were straightforward. image.GetBuffer() is returning a Python "str" type. numpy.frombuffer is expecting a bytes like object. I chose the "latin-1" codec for conversion from str->bytes because I believe this will convert ord(0..255) to bytes 0x00..0xFF. However, in my test case the image.GetBuffer()[14528] has an ord of 56461 which causes an exception. If the gdcm buffer is a char* I am not sure how this is possible. I looked at the gdcm C++ code some and in places there is some dynamic typing that might be difficult for SWIG to determine?
I cannot provide my test DICOM image because it contains personal data. Using a C++ program I was successful at extracting a raw image and then converting the raw to PNG. Therefore I believe the input data is valid. I believe this test image in DICOM is JPEG 2000 Compression (Lossless only), 1036x825, dtype int8, buffer length 854700.
Any suggestions on how to debug this? Different codec? Different way to convert str to bytes?