NIfTI-1 or NIfTI-2 Data format

Could you please advise which data format is used when saving image file e.g. with GetImageFromArray(voxel_buffer) and ImageFileWriter() NIfTI-1 or NIfTI-2 ? is there posibility to specify explicitly data format (NIfTI-1 or NIfTI-2) in which save .nii image ?

Good question, I believe it is nifti1 but not sure.

Trivial difference between the two is in the first four bytes sizeof_hdr, 348 for nifti1, 540 for nifti2 so checking that will let you know what format you are dealing with:

file_name = 'test.nii'
nii1_sizeof_hdr = 348
nii2_sizeof_hdr = 540

with open(file_name, 'rb') as fp:
    byte_data = fp.read(4)
    sizeof_hdr = int.from_bytes(byte_data, byteorder='little')
    if sizeof_hdr == nii1_sizeof_hdr:
        print('nifti1')
    elif sizeof_hdr == nii2_sizeof_hdr:
        print('nifti2')
    else: #big endian
        sizeof_hdr = int.from_bytes(byte_data, byteorder='big')
        if sizeof_hdr == nii1_sizeof_hdr:
            print('nifti1')
        elif sizeof_hdr == nii2_sizeof_hdr:
            print('nifti2')
1 Like

Thank you very much for your reply! yes I have checked that it is nifti1 , but still not sure whether it is possible to save image in nifti2 data format with the help of ITK ?

Judging by the code, ITK only writes NIFTI-1 file format. Not sure about reading support for version 2.

1 Like

Thanks a lot for the reply!