Create Numpy array of images

前端 未结 4 1636
臣服心动
臣服心动 2021-01-31 06:01

I have some (950) 150x150x3 .jpg image files that I want to read into an Numpy array.

Following is my code:

X_data = []
files = glob.glob (\"*.jpg\")
fo         


        
4条回答
  •  猫巷女王i
    2021-01-31 06:13

    Appending images in a list and then converting it into a numpy array, is not working for me. I have a large dataset and RAM gets crashed every time I attempt it. Rather I append the numpy array, but this has its own cons. Appending into list and then converting into np array is space complex, but appending a numpy array is time complex. If you are patient enough, this will take care of RAM crasing problems.

    def imagetensor(imagedir):
      for i, im in tqdm(enumerate(os.listdir(imagedir))):
        image= Image.open(im)
        image= image.convert('HSV')
        if i == 0:
          images= np.expand_dims(np.array(image, dtype= float)/255, axis= 0)
        else:
          image= np.expand_dims(np.array(image, dtype= float)/255, axis= 0)
          images= np.append(images, image, axis= 0)
      return images
    

    I am looking for better implementations that can take care of both space and time. Please comment if someone has a better idea.

提交回复
热议问题