1

I'm trying to reshape an image after reshaping it, I'm facing problems when it comes to the saving method. Here's the code I'm trying to run:

import nibabel as nib
import numpy as np
from nibabel.testing import data_path
import os
example_filename = os.path.join("D:/Volumes convertidos LIDC", 
'teste001converted.nii.gz')
img = nib.load('teste001converted.nii.gz')
print (img.shape)
newimg = img.get_fdata().reshape(332,360*360)
print (newimg.shape)
final_img = nib.Nifti1Image(newimg, img.affine)
nib.save(final_img, os.path.join("D:/Volumes convertidos LIDC", 
'test2d.nii.gz'))

And I'm getting an error:

(most recent call last):

File "d:\Volumes convertidos LIDC\reshape.py", line 17, in final_img = nib.Nifti1Image(newimg, img.affine)

File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1756, in init super(Nifti1Pair, self).init(dataobj,

File "C:\Python39\lib\site-packages\nibabel\analyze.py", line 918, in init super(AnalyzeImage, self).init( File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 469, in init self.update_header()

File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 2032, in update_header super(Nifti1Image, self).update_header() File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1795, in update_header super(Nifti1Pair, self).update_header()

File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 496, in update_header hdr.set_data_shape(shape)

File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 880, in set_data_shape super(Nifti1Header, self).set_data_shape(shape)

File "C:\Python39\lib\site-packages\nibabel\analyze.py", line 633, in set_data_shape

raise HeaderDataError(f'shape {shape} does not fit in dim datatype')

nibabel.spatialimages.HeaderDataError: shape (332, 129600) does not fit in dim datatype

Is there any way to solve it?

  • I don't think this question should flagged as a duplicate. As for as I am concerned, this particular error message has not been mentioned in any other question at SO. – Jafar Isbarov Jan 12 '22 at 17:52
  • Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – gre_gor Jan 13 '22 at 13:53
  • I just changed, thank you @gre_gor – Mauricésar Barbosa Jan 13 '22 at 17:14

1 Answers1

3

You are trying to save a numpy array, whereas the nib.save expects a SpatialImage object.

You should convert the numpy array to a SpatialImage:

final_img = nib.Nifti1Image(newimg, img.affine)

After which you can save the image:

nib.save(final_img, os.path.join("D:/Volumes convertidos LIDC", 'test4d.nii.gz'))

See the documentation and this answer for more explanation.

Edit: This will not work if newimg is a 2D image.

Jafar Isbarov
  • 1,363
  • 1
  • 8
  • 26
  • Thank you for you input, unfortunately your solution is not working, I'm getting this error: HeaderDataError: shape (332, 129600) does not fit in dim datatype – Mauricésar Barbosa Jan 12 '22 at 21:11
  • @MauricésarBarbosa please edit your question and add the complete code and errors, so I can check it. – A.Najafi Jan 13 '22 at 07:30
  • I just did @A.Najafi – Mauricésar Barbosa Jan 13 '22 at 13:20
  • You have loaded a 3D image and reshaped it into a 2D image. That might be the problem. Apparently, either `NiftiImage` does not accept 2D images, `img.affine` has some 3D information that does not match with `newimg`. – Jafar Isbarov Jan 13 '22 at 13:23
  • The following NiBabel tutorial explicitly states this: "The image data array: a 3D or 4D array of image data." https://nipy.org/nibabel/coordinate_systems.html – Jafar Isbarov Jan 13 '22 at 13:25