Search code examples
pythonsubprocessdicompydicom

How to call command line command (AFNI command)?


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.


Solution

  • 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.