Saving image after loading as_closest_canonical swaps axis
bug
I have noticed that in some cases (not always) saving an image after being loaded using `nib.as_closest_canonical` and then re-loading it in the same way causes axis to be transposed. To illustrate this the attached image and the code below can be used.
```python
import nibabel as nib
from pathlib import Path
f = Path("test.nii.gz")
image = nib.as_closest_canonical(nib.load(f))
nib.save(nib.Nifti1Image(image.get_fdata(), image.affine, image.header), "test-canonical.nii.gz")
image_reloaded = nib.as_closest_canonical(nib.load("test-canonical.nii.gz"))
print(image.shape, image_reloaded.shape)
# (1024, 1024, 33) (33, 1024, 1024)
```
Expected behavior:
The axis order should be consistent after saving an image and re-loading it.
Exemplary image:
[test.nii.gz](https://github.com/user-attachments/files/24040999/test.nii.gz)
关闭于 2026-01-14 7 条评论