1

I am programming a code that uses 30 images and I want to put those images in an array to resize them and then use them in other functions. I was trying some things but the second loop just shows me one unique image resized 30 times.

    import cv2 as cv
    import glob
    import numpy as np


    files = glob.glob ("C:/Users/Project/imgs/*.jpg")
    images = np.empty(len(files))

    #This loop reads images and show rgb images
    #Works OK
    for i in files:
        #print(myFile)
        images= cv.imread(i)
        cv.imshow('myFile'+str(i),images)


    new = []
    for i in range(30):
        new = cv.resize(images,(200,266))
        cv.imshow('imagen', new)

    cv.waitKey(0)
    cv.destroyAllWindows()
2
  • Your resize loop needs to be inside the files loop. So indent everything below new = [] Commented May 24, 2020 at 2:40
  • if you use list new = [] then you have to use new.append( ... ) to add element to list. And then you may use new[-1] to access last image on list - cv.imshow('imagen', new[-1]). Maybe use better name for list - ie. all_images instead of new Commented May 24, 2020 at 2:46

1 Answer 1

1

If you want to keep many elements then first create empty list and next use apppend() to add element to list.

More or less

all_images = []

for name in files:
    #print(name)
    image = cv.imread(name)
    cv.imshow('myFile '+name, image) # you don't need `str()`
    all_images.append(image)


resized_images = []

for image in all_images:
    new = cv.resize(image, (200,266))
    cv.imshow('imagen', new)
    resized_images.append(new)

If you want to resize only first 30 images

for image in all_images[:30]:
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but the code just show me one resized image with that.
Now it works fine! Your solution was working but with cv.imshow('imagen', new)we just put one name for all images and the program just shows a window with that name. The solution was to use a string that changes in the loop to assign the names for resized images.

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.