0

I am new to the medical image processing. I am trying to read a Nifti image and write the header information into a csv file.

import nibabel as nib
img = nib.load("xxx.hdr.gz")
hdr = img.header
print(hdr)

<class 'nibabel.nifti1.Nifti1PairHeader'> object, endian='>'
sizeof_hdr      : 348
data_type       : b''
db_name         : b''
extents         : 0
session_error   : 0
regular         : b'r'
dim_info        : 0
dim             : [  3  91 109  91   1   1   1   1]
...............

Now how do i manage to save this information into a csv file? When i get the data into Numpy it is a 3 Dimensional numpy but what is the right way to save this information into a csv format.

1 Answers1

1

At the moment I assume you want to have all header information in the same style as you can see when printing hdr exported into a csv. This you can do my creating a list of headers and then export it with the csv python module.

import nibabel as nib
import csv

img = nib.load("xxx.hdr.gz")
hdr = img.header
print(hdr)

# Get all header data into list.
header = [(_key) for _key in nib_file.header]
headers_data = []
headers_data.append([img.header[_key] for _key in img.header])

# Write all headers from header into csv.
csv_file = open('headers.csv', mode='w')
csv_file_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_file_writer.writerow(header)
for header_data in headers_data:
    csv_file_writer.writerow(header_data)

csv_file.close()
ced-mos
  • 178
  • 1
  • 8