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?
1 Answer
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)
1 Comment
lceans
So say I have nested for loops(sorry can't post the code at work right now) but for example: for x in range(0,10,1) then for y in range (0,3,1) and I want to save the image for through each iteration of the loops. Can I access the image based on the x and y like I want to see the image where x = 3 and y = 2? If so how can I do it?
images = []outside of the loop and appending it during the loopimages.append(image)is all that you need.for img in images:Or you can access its individual items:first_img = images[0],second_img = images[1]I assume the first option is what you're after.