0

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?

7
  • I assume that creating a list images = [] outside of the loop and appending it during the loop images.append(image) is all that you need. Commented Jun 8, 2020 at 23:47
  • Great I will try that. Sorry also new to python so when I do that. How do I access the images individually later? Commented Jun 8, 2020 at 23:52
  • 1
    You can loop through the list: 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. Commented Jun 9, 2020 at 0:03
  • 1
    You can read about python lists here: geeksforgeeks.org/python-list Commented Jun 9, 2020 at 0:04
  • Right but can I still show the image that way? Like to see the actual image not just the array values Commented Jun 9, 2020 at 0:15

1 Answer 1

1

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.