vanetoj
vanetoj

Reputation: 99

How to read in a directory of images with OpenCV

I'm trying to read this directory of jpg images and add them to a list but the list is empty when I run this code:

import glob
import cv2
cv_img = []
for img in glob.glob("E:/project/file/*.jpg"):
    n= cv2.imread(img)
    cv_img.append(n)

Upvotes: 1

Views: 1845

Answers (1)

nathancy
nathancy

Reputation: 46600

You can use a list comprehension to read each image into a list. Also ensure that the relative path you're passing in to glob exists otherwise the list may be empty

import cv2
import glob

images = [cv2.imread(image) for image in glob.glob("images/*.jpg")]

for image in images:
    cv2.imshow('image', image)
    cv2.waitKey(1000)

Upvotes: 1

Related Questions