You can initialize the output array of this form and once inside the loop, index on the first axis to assign image arrays iteratively -
out = np.empty((K,M,N,3), dtype=np.uint8) # change dtype if needed
for i,filename in enumerate(glob.glob(path+"/*.ppm")):
# Get img of shape (M,N,3)
out[i] = img
If you do not know Kin advance, we can get it using len(glob.glob(path+"/*.ppm")).
source
share