How can I obtain all 2D slices from a 3D image at once?

I'm using the following code to slice a 3D image:

import nibabel as nib
import numpy as np
from nibabel.testing import data_path
import os

vol1= np.load("teste01.npy")
zSlice= (vol1[200, :, :]).squeeze()
print (zSlice.shape)
np.save("D:/Volumes convertidos LIDC/slice200.npy", zSlice)

The problem is that I need to do it manually, I need to obtain all slices and there are just to many for it to be possible to keep doing it like that. Is there any alternative?


If I understand correctly what you want to do, the following should work:

for i, s in enumerate(vol1):
    np.save(f"D:/Volumes convertidos LIDC/slice{i}.npy", s)

This will save each 2-dimensional slice taken along the 0-th axis in a separate file (which can mean a lot of files)