I have a simple question. i need to resize CT scan saved in nifty file. I need to add one black slice at the end of the 3D image (in Z direction).
I would appreciate for any help/suggestion. How can I do that in simpleitk ?
Is there any simple way to modify that image ?
“one black slice” - this is not well defined as black is a display color, not an intensity value. As you are working with CT, likely you want to add a slice with air, which has a HU value of -1000 (often people equate “black” with 0 intensity).
Images in ITK/SimpleITK are spatial objects, so padding an image involves modifications to the spatial information.
Code below does what you want (significantly simplified based on the feedback below from @blowekamp). It can be further simplified to a one liner if you know which side you want to pad, wasn’t clear to me so this is a flag:
import SimpleITK as sitk
file_name =
image = sitk.ReadImage(file_name)
air_hu = -1000
# which end of the slice stack to pad front or back
front_end = True
if front_end:
padded_image = sitk.ConstantPad(image, [0,0,1], [0,0,0], air_hu)
else:
padded_image = sitk.ConstantPad(image, [0,0,0],[0,0,1], air_hu)