for your first question, you should check the orientation of your NIfTI image and then rotate it in the direction that you want.
for checking orientation we use nibabel aff2axcodes https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.aff2axcodes
for the rotation we will use nibabel.orientations.flip_axis https://nipy.org/nibabel/reference/nibabel.orientations.html#nibabel.orientations.flip_axis
'''
def check_orientation(ct_image, ct_arr):
"""
Check the NIfTI orientation, and flip to 'RPS' if needed.
:param ct_image: NIfTI file
:param ct_arr: array file
:return: array after flipping
"""
x, y, z = nib.aff2axcodes(ct_image.affine)
if x != 'R':
ct_arr = nib.orientations.flip_axis(ct_arr, axis=0)
if y != 'P':
ct_arr = nib.orientations.flip_axis(ct_arr, axis=1)
if z != 'S':
ct_arr = nib.orientations.flip_axis(ct_arr, axis=2)
return ct_arr
'''
Note that in your case you need to flip only the z axis, but try to use the function above and change to your situation in order to check your data.
The last thing is to save the new NIfTI scan (NumPy array) as nii image.
In order to do it, use Nifti1Image to create nii object, and then nibabel.save to save it to a file.
'''
new_nifti = nib.Nifti1Image(***ct_arr***.astype(np.float), nii_original_scan.affine)
nib.save(new_nifti, f'***path to new scan***.nii.gz')
'''
ct_arr- is your numpy array (make sure is in float type).
The second argument is the original NIfTI scan after loading.
in the save function- make sure you add in the file name the suffix .nii or .nii.gz
Hope it's clear :)