How to save processed slices into a new nifti file

Hi, All

Given an input nifti image, in.nii.gz, if I intend to crop it from 1/5 slices along z dimension and save the cropped image into a new nifti file with same header except dimensions, how can I do it.
I only figure out how to crop but am not sure about updating the header and save it. Can someone shed a light on this? Thanks.

img = nib.load('in.nii.gz')
data = img.get_data()
nbrSlces = data.shape[2]
nbrCrops = int(nbrSlces * 0.20)
crop = data[:, :, nbrCrops:]
img.__class__(crop, img.affine, img.header).to_filename('cropped.nii.gz')

although I believe you may want to update the origin of coordinates in the affine if the direction along Z is not positive.

MANAGED BY INCF