student
student

Reputation: 23

How implement an array of images in Python-opencv?

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()

Upvotes: 1

Views: 1771

Answers (1)

furas
furas

Reputation: 142631

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]:

Upvotes: 1

Related Questions