Skip to content

Instantly share code, notes, and snippets.

@xgrg
Last active May 31, 2017 11:04
Show Gist options
  • Save xgrg/d74133ae693fe98cd1a0dae58aace346 to your computer and use it in GitHub Desktop.
Save xgrg/d74133ae693fe98cd1a0dae58aace346 to your computer and use it in GitHub Desktop.
Arithmetics on images with nibabel/numpy
import numpy as np
import nibabel as nib
im1 = nib.load('/tmp/path_to_image.nii.gz')
a = np.array(im1.dataobj)
im2 = nib.Nifti1Image(a/2.0, header=im1.header, affine=im1.affine)
im2.to_filename('/tmp/path_to_new_image.nii.gz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment