0

I have this code:

image_list = []
for filename in glob.glob('C:\\Users\\Utilizador\\Desktop\\menu\\*.jpg'): 
    im=cv2.imread(filename)
    image_list.append(im)

This creates me a list of images, but I need it to be in an array form with shape (num_of_images, width, height, 3)

All images have the same shape Any ideas? Thanks

1
  • yes they have the same shape Commented Jun 21, 2018 at 10:41

1 Answer 1

1

Since all images have same shape, we can create an empty array and then read each image into that.

In [103]: file_path = 'C:\\Users\\Utilizador\\Desktop\\menu\\*.jpg'
In [104]: num_imgs = len(glob.glob(file_path))
In [105]: width, height, channels = 512, 512, 3

In [106]: batch_arr = np.empty((num_imgs, width, height, channels), dtype=np.uint8)

In [107]: for idx, filename in enumerate(glob.glob(file_path)):
              img = cv2.imread(filename)
              # if img is of different width and height than defined above
              # do some resize and then insert in the array.
              batch_arr[idx] = img
Sign up to request clarification or add additional context in comments.

3 Comments

That works, but I would like something more dynamic, where the number of images, width, height would not be fixed, like a way to read the images and just append them as an array instead of a list
But will it be possible to then edit the images, like resizing them for example inside the array? Or should I do it before I add them to the array?
there is an issue, the original image is uint8 format and when I append to the array, it becames float64 and this messes up the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.