Search code examples
pythonswigdicomgdcm

GDCM python DICOM decompression


I executed this python script. An error happened in the line

t = gdcm.Orientation.GetType(dircos)

The error information is:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-8-fb43b0929780>", line 1, in <module>
    gdcm.Orientation.GetType(dircos)
TypeError: expected a list.

I looked up the classes reference. It says

Input is an array of 6 double

The variable dircos is exactly a list with 6 elements,

>>> dircos
Out[11]: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]

I don't know why it goes wrong.


Solution

  • I checked the source code and found that it actually check for tuple. The message is misleading.

    // Grab a 6 element array as a Python 6-tuple
    %typemap(in) const double dircos[6] (double temp[6]) {   // temp[6] becomes a local variable
      int i;
      if (PyTuple_Check($input) /*|| PyList_Check($input)*/) {
        if (!PyArg_ParseTuple($input,"dddddd",temp,temp+1,temp+2,temp+3,temp+4,temp+5)) {
          PyErr_SetString(PyExc_TypeError,"list must have 6 elements");
          return NULL;
        }
        $1 = &temp[0];
      } else {
        PyErr_SetString(PyExc_TypeError,"expected a list.");
        return NULL;
      }
    }
    

    You need to pass a tuple:

    >>> import gdcm
    >>> dircos = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
    >>> gdcm.Orientation.GetType(tuple(dircos))
    1