简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM