Search code examples
python-3.xdicom

Dicom Image error


I am trying to read a dicom image with the following code:

import dicom
from PIL import Image

Ima = dicom.read_file("MR000000.dcm")

f = Ima.pixel_array

sa = Image.fromarray(f)
sa.show()

But I get the following error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1926, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1), '<u2')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "lala.py", line 8, in <module>
    sa = Image.fromarray(f)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1929, in fromarray
    raise TypeError("Cannot handle this data type")
TypeError: Cannot handle this data type

If anyone knows how to solve, thank you.


Solution

  • I think it might be as simple as your version of PIL does not support 16-bit image types. The numpy type string "<u2" is a "uint16", which is most likely the bit depth of you dicom image. If you are using genuine PIL, you might want to switch to Pillow