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?