lceans
lceans

Reputation: 191

Is there a way to save multiple images to an array in python for later use?

I am using opencv python. I have a nested for loop that generates an image with each iteration. Is there a way to save these images to an array for view later or a way to save each one individually through each iteration?

Upvotes: 0

Views: 5306

Answers (1)

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4352

Yes, it is possible. You can use simply append() function to add your each image to an array.

Let assume you have some images in the file in *.jpg format. In a loop you can read each image and append them to an array. After loop done, you can call desired image from array and show them with imshow()

Here is an example code:

import cv2
import glob
import numpy as np


image_array = [] // array which ll hold the images
files = glob.glob ("*.jpg")
for myFile in files:
    image = cv2.imread (myFile)
    image_array.append (image) // append each image to array
// this will print the channel number, size, and number of images in the file
print('image_array shape:', np.array(image_array).shape) 

cv2.imshow('frame', image_array[0])

cv2.waitKey(0) 

Upvotes: 1

Related Questions