I’m trying to perform a very simple operation on an image using Python. I just want to read it as a NumPy array, change some values and then write it back as a new image. The problem is: when I write it back, I lose the original origin and orientation (that’s a mouthful). Is there any way I can keep them? The code is as simple as this:
import numpy as np import itk input_filename = "mask.nii" input_img = itk.imread(input_filename) np_copy = itk.GetArrayFromImage(input_img) np_copy[np_copy != 0] = 255 itk_nb_img = itk.GetImageFromArray(np_copy) itk.imwrite(itk_nb_img, "output_mask.nii")
I’ve uploaded a working example to make it easier to understand. Do any of you know how to deal with this? Thanks in advance!
itk_python.zip (4.6 KB)