- #1
BRN
- 108
- 10
Hello everyone,
I have to extract a slice from a nii files and resize it with dimensions 256x256. Once this is done, I have to save it as a PNG image.
This is my code:
The problem is that PNG files are correctly created, but if you check the image size I do not get 256x256. How is it possible?
How can I solve?
Thanks.
I have to extract a slice from a nii files and resize it with dimensions 256x256. Once this is done, I have to save it as a PNG image.
This is my code:
slice from nii file:
def img_from_nii(height, width, n_slice, label, in_path, temp_path):
filenames = os.listdir(in_path)
for i in range(len(filenames)):
mri_file = in_path + filenames[i]
img_data = nib.load(mri_file).get_fdata()
img_data = np.transpose(img_data, (2, 1, 0))
slice_2D = Image.fromarray(img_data[:, :, n_slice]).resize((height, width))
resized_slice = plt.matshow(slice_2D, cmap = 'gray', fignum = 0)
plt.axis('off')
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
plt.margins(0, 0)
plt.savefig(temp_path + label + '_ADNI_' + 'slc' + str(n_slice) + '_' + str(i + 1) + '.png')
plt.close()
print('dataset done!')
The problem is that PNG files are correctly created, but if you check the image size I do not get 256x256. How is it possible?
check size:
im = Image.open('./outputs/ADNI_png_temp/P_ADNI_slc150_3.png')
width, height = im.size
print('width:', width)
print('height:', height)
outputs:
width: 432
height: 288
How can I solve?
Thanks.