GoC
GoC

Reputation: 31

Store an image in an array using Python

I need to read images (500x300x3) and store them in arrays:

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

now here error occurs because the size of arr is 500x300x3, but I want to store it as np.empty((number_of_images, 3, 300, 500), dtype="float32").

Any suggestions will be greatly appreciated!

Upvotes: 0

Views: 2346

Answers (1)

eumiro
eumiro

Reputation: 212835

Transpose the array:

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

Upvotes: 1

Related Questions