Skip to content

Instantly share code, notes, and snippets.

@ofgulban
Created November 12, 2016 13:47
Show Gist options
  • Select an option

  • Save ofgulban/46d5c51ea010611cbb53123bb5906ca9 to your computer and use it in GitHub Desktop.

Select an option

Save ofgulban/46d5c51ea010611cbb53123bb5906ca9 to your computer and use it in GitHub Desktop.
Convert MINC files to Nifti using nibabel.
"""Convert minc to nifti format."""
import os
import numpy as np
from nibabel import load, save, Nifti1Image
minc = load("/path/to/file.mnc.gz")
basename = minc.get_filename().split(os.extsep, 1)[0]
affine = np.array([[0, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, 1]])
out = Nifti1Image(minc.get_data(), affine=affine)
save(out, basename + '.nii.gz')
@neurolabusc
Copy link
Copy Markdown

This is an elegant minimal gist, but by using an identity matrix for the affine the spatial orientation is lost. The script mni2nii.py extends this gist to include spatial transformations.

@ofgulban
Copy link
Copy Markdown
Author

ofgulban commented Mar 3, 2025

That's right. Thanks for your comment and pointer @neurolabusc !

I saw that I have created this gist 9 years ago, time flies by.

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