Skip to content

Instantly share code, notes, and snippets.

@jcreinhold
Created December 29, 2018 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcreinhold/a26d6555b0e7aa28b79757f766640dd6 to your computer and use it in GitHub Desktop.
Save jcreinhold/a26d6555b0e7aa28b79757f766640dd6 to your computer and use it in GitHub Desktop.
Convert mha file format to NIfTI
#/usr/bin/env python
# -*- coding: utf-8 -*-
"""
nii_to_tif
convert all mha files in a directory to nifti
Author: Jacob Reinhold (jacob.reinhold@jhu.edu)
"""
import argparse
from glob import glob
import os
import sys
import itk
def arg_parser():
parser = argparse.ArgumentParser(description='convert all mha images in a directory to nifti')
parser.add_argument('img_dir', type=str)
parser.add_argument('out_dir', type=str)
return parser
def split_filename(filepath):
path = os.path.dirname(filepath)
filename = os.path.basename(filepath)
base, ext = os.path.splitext(filename)
if ext == '.gz':
base, ext2 = os.path.splitext(base)
ext = ext2 + ext
return path, base, ext
def main():
try:
args = arg_parser().parse_args()
fns = glob(os.path.join(args.img_dir, '*.mha'))
if len(fns) == 0:
raise Exception(f'Could not find .mha files in {args.img_dir}')
for fn in fns:
print(f'Converting image: {fn}')
img = itk.imread(fn)
_, base, _ = split_filename(fn)
out_fn = os.path.join(args.out_dir, base + '.nii.gz')
itk.imwrite(img, out_fn)
print(f'Saved to: {out_fn}')
return 0
except Exception as e:
print(e)
return 1
if __name__ == "__main__":
sys.exit(main())
@shahrzad-shapoori
Copy link

shahrzad-shapoori commented Dec 20, 2019

I get this error:

"File "mha_to_nii.py", line 41
    raise Exception(f'Could not find .mha files in {args.img_dir}')
                                                                 ^
SyntaxError: invalid syntax"

do u know what could have gone wrong?

@jcreinhold
Copy link
Author

@shahrzad-shapoori This script requires python 3.6+. You are probably receiving the error because of the f-string. Either change that to a .format or whatever or update your python version. If you still receive the error, please report back with your python version number and the exact error message if it is different.

@shahrzad-shapoori
Copy link

Thanks! I upgraded my Python and it worked :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment