1

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)
nathancy
  • 42,661
  • 14
  • 115
  • 137
vanetoj
  • 103
  • 11

1 Answers1

1

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)
nathancy
  • 42,661
  • 14
  • 115
  • 137
  • but i need them in a list. .. and nothing is showing anyways – vanetoj Oct 15 '19 at 20:58
  • The images are in the list, if you `print(len(images))`, you should get the number of loaded images. Your path directory is probably incorrect or invalid so its not reading in any images. Note the path is a relative path – nathancy Oct 15 '19 at 21:02
  • well i tried length already and it was empty . guess check on the path . it maybe cuz in jupyter notebook. – vanetoj Oct 16 '19 at 22:02
  • I'm 99% sure its a problem with either the path or the mage does not exist – nathancy Oct 17 '19 at 01:22