How to load mutiple PPM files present in a folder as single Numpy ndarray?

大城市里の小女人 提交于 2019-12-13 09:54:28

问题


The following Python code creates list of numpy array. I want to load by data sets as a numpy array that has dimension K x M x N x 3 , where K is the index of the image and M x N x 3 is the dimension of individual image. How can I modify the existing code to do so ?

    image_list=[]
    for filename in glob.glob(path+"/*.ppm"):
        img = imread(filename,mode='RGB')
        temp_img = img.reshape(img.shape[0]*img.shape[1]*img.shape[2],1)
        image_list.append(temp_img)

回答1:


You could initialize an output array of that shape and once inside the loop, index into 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 don't know K beforehand, we could get it with len(glob.glob(path+"/*.ppm")).



来源:https://stackoverflow.com/questions/46046459/how-to-load-mutiple-ppm-files-present-in-a-folder-as-single-numpy-ndarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!