Skip to content

Instantly share code, notes, and snippets.

@sarthakpati
Last active May 20, 2021 15:17
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 sarthakpati/73c8ffd7586e715680f3679a791bbec3 to your computer and use it in GitHub Desktop.
Save sarthakpati/73c8ffd7586e715680f3679a791bbec3 to your computer and use it in GitHub Desktop.
import SimpleITK as sitk
# read image
inputImage = sitk.ReadImage('/path/to/input.nii.gz')
# get result in the form of a numpy array
npa_res = my_algorithm(sitk.GetArrayFromImage(inputImage)) # my_algorithm does something fancy
# Converting back to SimpleITK (assumes we didn't move the image in space as we copy the information from the original)
result_image = sitk.GetImageFromArray(npa_res)
result_image.CopyInformation(inputImage)
# write the image
sitk.WriteImage(result_image, '/path/to/result.nii.gz')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment