简体   繁体   中英

Using Python and OpenCv to sequentially display all the images in a directory

I'm trying to use OpenCV's python bindings to sequentially display all of the images in a directory.

This code prints all the files in my directory but only displays the first image it opens:

for file in os.listdir(path):
  print(file)

  image = cv2.imread(os.path.join(my_dir, 'attachments',
  file))

  cv2.imshow("Image", image)
  cv2.waitKey(550)

What do I need to change so that every image in my directory is displayed by cv2.imshow? My print statement shows me that I am looping through all of my images but imshow is not being redrawn through any but the first iteration of my loop.

EDIT:

If you want to see all your images in different windows:

The other thing that occurred to me is that you may want to give each of your cv2.imshow() commands a different name other than "Image". With different names, you can have multiple imshow windows. So you could do:

for indx, f in enumerate(os.listdir(path), start=1):
    image = cv2.imread(os.path.join(my_dir, 'attachments', f))  
    cv2.imshow("Image-{}".format(indx), image)
    cv2.waitKey(600)

EDIT 2:

I went back to double check my answer and realized that your code seems to work just fine for me... I'm using opencv '3.0.0'

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