Building on our previous example of opening a single image, this guide will explain how to open multiple images. The easiest way to load multiple images is to put all the images into a single folder and loop through the directory opening each one.
This guide doesn’t introduce any new OpenCV functions you shouldn’t already be familiar with from the previous example, but we will include new packages os and os.path to make a list of images to process.
The code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# import the necessary packages import cv2 import os, os.path #debug info OpenCV version print ("OpenCV version: " + cv2.__version__) #image path and valid extensions imageDir = "images/" #specify your path here image_path_list = [] valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here valid_image_extensions = [item.lower() for item in valid_image_extensions] #create a list all files in directory and #append files with a vaild extention to image_path_list for file in os.listdir(imageDir): extension = os.path.splitext(file)[1] if extension.lower() not in valid_image_extensions: continue image_path_list.append(os.path.join(imageDir, file)) #loop through image_path_list to open each image for imagePath in image_path_list: image = cv2.imread(imagePath) # display the image on screen with imshow() # after checking that it loaded if image is not None: cv2.imshow(imagePath, image) elif image is None: print ("Error loading: " + imagePath) #end this loop iteration and move on to next image continue # wait time in milliseconds # this is required to show the image # 0 = wait indefinitely # exit when escape key is pressed key = cv2.waitKey(0) if key == 27: # escape break # close any open windows cv2.destroyAllWindows() |
A note on OpenCV image support from the OpenCV docs (http://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html)
Note OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 – codenamed in the CMake as Jasper), TIFF files (tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.
Boilerplate code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import cv2 import os, os.path print (cv2.__version__) imageDir = "images/" #specify your path here image_path_list = [] valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here valid_image_extensions = [item.lower() for item in valid_image_extensions] for file in os.listdir(imageDir): extension = os.path.splitext(file)[1] if extension.lower() not in valid_image_extensions: continue image_path_list.append(os.path.join(imageDir, file)) for imagePath in image_path_list: img = cv2.imread(imagePath) if img is None: continue cv2.imshow(imagePath, img) key = cv2.waitKey(0) if key == 27: # escape break cv2.destroyAllWindows() |