-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transfer_vmr_to_nii.py
55 lines (40 loc) · 1.52 KB
/
Transfer_vmr_to_nii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 28 03:39:55 2022
@author: gangxinli
"""
import os
import numpy as np
import nibabel as nb
import vmr
# FILE = "/home/faruk/Documents/test_bvbabel/derivatives/T1_int16.vmr"
# # =============================================================================
# # Load vmr
# header, data = bvbabel.vmr.read_vmr(FILE)
# # See header information
# pprint.pprint(header)
# # Export nifti
# basename = FILE.split(os.extsep, 1)[0]
# outname = "{}_bvbabel.nii.gz".format(basename)
# img = nb.Nifti1Image(data, affine=np.eye(4))
# nb.save(img, outname)
# print("Finished.")
def vmr_to_nii(path = "/Users/gangxinli/Desktop/Internship/Neuro/Neuro_ISC/Data/vtc_vmr"):
print("Transfering vmr file to nii file")
for root,dirs,files in os.walk(path):
for file in files:
if file.split(".")[-1] == 'vmr':
FILE = os.path.join(root,file)
header, data = vmr.read_vmr(FILE)
# Save nifti for testing
basename = FILE.split(os.extsep, 1)[0]
outname = "{}.nii.gz".format(basename)
# Export nifti (assign an identity matrix as affine with default header)
img = nb.Nifti1Image(data, affine=np.eye(4))
nb.save(img, outname)
print(os.path.join(root,file)+"\nComplete")
print("Convert Complete!")
return path
if __name__ =="__main__":
vmr_to_nii('/Users/gangxinli/Desktop/Internship/Neuro/Neuro_ISC/Data/30Aug/1546')