使用Python将图像存储在数组中

时间:2015-12-04 10:29:34

标签: python arrays numpy

我需要读取图像(500x300x3)并将它们存储在数组中:

import os
import numpy as np

data = np.empty((number_of_images, 3, 300, 500), dtype="float32")

imgs = os.listdir("./images")
num = len(imgs)
for i in range(num):
    img = Image.open("./images/" + imgs[i])
    arr = np.asarray(img, dtype="float32")
    data[i, :, :, :] = arr

现在出现此错误是因为arr的大小为500x300x3,但我想将其存储为np.empty((number_of_images, 3, 300, 500), dtype="float32")

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

Transpose数组:

data[i, :, :, :] = arr.transpose(2, 1, 0)