Why is there filesize difference when splitting and concatenating nifiti images using nilearn?

Basically I have a file called “HCP.nii.gz” which has shape of (104, 104, 72, 478) and I spitted this volume nifti into 2 equal parts by implementing below steps

fileobj = nb.load("HCP.nii.gz")
type(fileobj) => <nibabel.nifti1.Nifti1Image at 0x25058cdb130>
niftiarray = fileobj.get_fdata()
niftiarray.shape
(104, 104, 72, 478)
trimmed = niftiarray[:,:,:,:239]
trimmed_img = nb.Nifti1Image(trimmed, fileobj.affine)
trimmed_img.shape=> (104, 104, 72, 239)
nb.save(trimmed_img,"trimmed_img_239_vol.nii.gz")
fileobj2 = nil.image.load_img("HCP.nii.gz")//same file as above just loading it using nilearn
print(fileobj2.shape) -> (104, 104, 72, 478)
final_trim_img = index_img(fileobj2 ,slice(239,478))
final_trim_img.shape -> (104, 104, 72, 239)
nb.save(final_trim_img,"pt_2_trimmed_img.nii.gz")

Question 1) why the file size is different though the shape of the nifti image is same?
pt_2_trimmed_img.nii.gz → file_size → 285MB
trimmed_img_239_vol.nii.gz → file_size → 430MB

Question 2) why the file size is not matching to the original file?

ct_vol = nb.funcs.concat_images((trimmed_img,final_trim_img),axis=3)
ct_vol.shape-> (104, 104, 72, 478)
nb.save(ct_vol,"ct_vol.nii.gz")

ct_vol.nii.gz->860MB

HCP.nii.gz->567MB

nibabel tends to promote data types which increases disk space requirements. Note that it can promote to data types that are not supported by other tools in our field. You should be aware of the recently merged aliases in nibabel and the intent to deprecate INT64 NIfTI images.

1 Like