0

I am using the SWIG generated Python wrappers for GDCM (comes with gdcm.py).

I am running the following Python3 script.

import gdcm
import sys

filename="path_to_data/gdcm_test.dcm"

r = gdcm.Reader()
r.SetFileName(filename)
r.Read()

f=r.GetFile()
ds = f.GetDataSet()

csa_t1 = gdcm.CSAHeader()

t1 = csa_t1.GetCSAImageHeaderInfoTag()

csa_t1.LoadFromDataElement(ds.GetDataElement( t1))
csa_t1.Print(sys.stdout)

The relevant snippet from the gdcmswig.py file (with the function that wraps Print) is below.

def Print(self, os: 'std::ostream &') -> "void":
    """
    void
    gdcm::CSAHeader::Print(std::ostream &os) const

    Print the CSAHeader (use only if Format == SV10 or NOMAGIC) 
    """
    return _gdcmswig.CSAHeader_Print(self, os)

The problem appears on the last line of my script. The call to Print(sys.stdout).

TypeError: in method 'CSAHeader_Print', argument 2 of type 'std::ostream &'

The problem, I think, is that Python’s sys.stdout is not the actual output file handle, but wraps the handle. What is the best way to solve this? Thanks in advance.

jacolas
  • 1
  • 1
  • You could create your own library and wrap the std::streams. See e.g. http://stackoverflow.com/questions/18860816/technique-for-using-stdifstream-stdofstream-in-python-via-swig. Otherwise, download the source of gdcm and see if the streams are aliased using some name – Jens Munk Aug 06 '15 at 18:09
  • `sys.stdout.fileno()` can return a real file descriptor though I don't know how it would help with passing `ostream&` argument. – jfs Aug 06 '15 at 22:35

0 Answers0