# NIfTI-1 or NIfTI-2 Data format

**URL:** https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330
**Category:** Uncategorized
**Tags:** nifti
**Created:** [August 13, 2021, 7:07am UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330 "2021-08-13T07:07:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Iryna\_Galytska](https://discourse.itk.org/user_avatar/discourse.itk.org/iryna_galytska/32/2212_2.png) [@Iryna\_Galytska](https://discourse.itk.org/u/Iryna_Galytska)
#### Post date: [August 13, 2021, 7:07am UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330/1 "2021-08-13T07:07:57Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 13, 2021, 1:09pm UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330/2 "2021-08-13T13:09:46Z")

</div>

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

Trivial difference between the two is in the first four bytes [sizeof\_hdr](https://nifti.nimh.nih.gov/pub/dist/doc/nifti2.h), 348 for nifti1, 540 for nifti2 so checking that will let you know what format you are dealing with:

```auto
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')

```

---

<div class="post-metadata">

### Author: ![Iryna\_Galytska](https://discourse.itk.org/user_avatar/discourse.itk.org/iryna_galytska/32/2212_2.png) [@Iryna\_Galytska](https://discourse.itk.org/u/Iryna_Galytska)
#### Post date: [August 13, 2021, 2:01pm UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330/3 "2021-08-13T14:01:38Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 13, 2021, 3:02pm UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330/4 "2021-08-13T15:02:13Z")

</div>

Judging by the [code](https://github.com/InsightSoftwareConsortium/ITK/blob/7777908fe897d7c8c4b90d9f3ca1ff33b8cff970/Modules/IO/NIFTI/src/itkNiftiImageIO.cxx#L1383-L1454), ITK only writes NIFTI-1 file format. Not sure about reading support for version 2.

---

<div class="post-metadata">

### Author: ![Iryna\_Galytska](https://discourse.itk.org/user_avatar/discourse.itk.org/iryna_galytska/32/2212_2.png) [@Iryna\_Galytska](https://discourse.itk.org/u/Iryna_Galytska)
#### Post date: [August 16, 2021, 8:08am UTC](https://discourse.itk.org/t/nifti-1-or-nifti-2-data-format/4330/5 "2021-08-16T08:08:00Z")

</div>

Thanks a lot for the reply!
