0

I am trying to read a dicom header tag in dicom file. Now, there are two ways to read this dicom header tag.

1) Using pydicom package in python which apparently is not working well on my python installed version(python 3).

2) or when i call AFNI function 'dicom_hinfo' through command line, i can get dicom tag value. The syntax to call afni function in terminal is as follows:

dicom_hinfo -tag aaaa,bbbb filename.dcm output:fgre

Now how should i call this dicom-info -tag aaaa,bbbb filename.dcm in python script. I guess subprocess might work but not sure about how to use it in this case.

Suever
  • 64,497
  • 14
  • 82
  • 101

2 Answers2

1

To get output from a subprocess, you could use check_output() function:

#!/usr/bin/env python
from subprocess import check_output

tag = check_output('dicom_hinfo -tag aaaa,bbbb filename.dcm output:fgre'.split(),
                   universal_newlines=True).strip()

universal_newlines=True is used to get Unicode text on Python 3 (the data is decoded using user locale's character encoding).

check_output() assumes that dicom_hinfo prints to its standard output stream (stdout). Some utilities may print to stderr or the terminal directly instead. The code could be modified to adapt to that.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Getting error. Couldn't interpret: Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 607, in check_output with Popen(*popenargs, stdout=PIPE, **kwargs) as process: File raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'dicom_hinfo' – learnningprogramming Feb 16 '15 at 14:45
  • 1
    @tryeverylanguage: [update your question](http://stackoverflow.com/posts/28533515/edit) and put the full traceback there (with proper formatting). The error means that there is no `dicom_hinfo` command in `PATH`. Either provide the full path to `dicom_hinfo` or add the corresponding directory to PATH envvar in the environment that runs the Python script. – jfs Feb 17 '15 at 02:07
0

Oh this was due to syntax error using Pydicom. I wanted to access 0019, 109c tag.

Syntax should be:

ds[0x0019,0x109c].value.

not ds[aaaa,bbbb].value