0

I'm trying to learn how to use pydicom for reading and processing dicom images. I'm using Python 3.

import dicom 
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)`

I get an error NameError: name 'pydicom' is not defined. If I change

   ds = pydicom.read_file(lstFilesDCM[0])

to

   ds = dicom.read_file(lstFilesDCM[0])

(using dicom.read_file instead), I get the following error:

NotImplementedError: Pixel Data is compressed in a format 
pydicom does not yet handle. Cannot return array

I also verified that pydicom is properly installed and updated.

How do i fix this?

Joe McMahon
  • 3,266
  • 21
  • 33
Kuni
  • 817
  • 1
  • 9
  • 24
  • You say `import dicom` and then try to use `pydicom`. Which is correct? – Peter Wood Nov 27 '17 at 21:58
  • I was doing this based on the documentation from pydicom http://pydicom.readthedocs.io/en/latest/working_with_pixel_data.html But either way, pydicom doesn't seem to be working. As @serafeim pointed out, it could be because of incompatible data. I thought the data is a standard format. Didn't think it'd be incompatible. I'll check and post what I find out. – Kuni Nov 27 '17 at 22:11
  • the error: Pixel Data is compressed in a format pydicom does not yet handle suggests that the data are not compatible with the library. if possible add some data to try find a solution – seralouk Nov 27 '17 at 22:13
  • Are there any documentation where it describes what kind of Pixel Data is compatible? This data is directly off of the Echocardiography machine. It's an ultrasound data. I'm trying to figure out how to check it's compatibility. – Kuni Nov 27 '17 at 22:16
  • There have been many compressed data handlers added to the working code in the repository, but these are not in an official release yet. To use the repository code (quite stable), install with `pip install git+https://github.com/pydicom/pydicom.git` and use `import pydicom`. – darcymason Nov 28 '17 at 01:46

2 Answers2

2

You are trying to call a class that you have not imported before:

Use:

import pydicom
import numpy


ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)

or

import dicom
ds = dicom.read_file("the_name_of_file.dcm")

Documentation: http://pydicom.readthedocs.io/en/stable/pydicom_user_guide.html

seralouk
  • 30,938
  • 9
  • 118
  • 133
  • I tried that too, but when I try to import it I get "No module named pydicom'. I'm using jupyter notebook and I even tried restarting Kernel. – Kuni Nov 27 '17 at 22:00
  • @have you installed dicom and pydicom ?? also see my modified answer – seralouk Nov 27 '17 at 22:01
  • and I was learing from this site: `http://pydicom.readthedocs.io/en/latest/working_with_pixel_data.html` and if you notice they did exactly what I had done. And I had also mentioned that in the question that I get `NotImplementedError' when I used `dicom.read_file` – Kuni Nov 27 '17 at 22:02
  • I cannot access this website. but can you try the 2nd thing that I suggested in my answer? also, make sure to read and use the official documentation. some other online tutorials contain errors quite often – seralouk Nov 27 '17 at 22:03
  • http://pydicom.readthedocs.io/en/latest/working_with_pixel_data.html As I mentioned above, I had already tried your second method, and get NotImplementedError. I edited my question to reformat it so that it's more visible. – Kuni Nov 27 '17 at 22:06
  • the second error suggests that your data are not compatible with the implemented methods of the library. can you add some data ? e.g. one dicom image? – seralouk Nov 27 '17 at 22:07
  • I would add the image, but it contains Patient info. i don't know DICOM enough to change anything there. I'll see if I can figure it out and post it here – Kuni Nov 27 '17 at 22:09
-1

If you want to get your hands on the pixel data, I suggest to use the convert program from the ImageMagick suite. You can either call this program from Python using the subprocess module. (See this example, where I convert them to JPEG format), or you can use one of the Python bindings.

If you want to manipulate the images, using the bindings might be preferable. But note that not all the bindings have been converted to ImageMagick version 7.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94