Download all images using imread from this folder

Downloading and saving images in OpenCV is quite limited, so ... what are the preferred ways to download all images from this folder? Should I search for files in this folder with extensions .png or .jpg, store names and use imread for each file? Or is there a better way?

+6
source share
5 answers

Why not just try downloading all the files in the folder? If OpenCV can't open it, fine. Go to the next one. cv2.imread() returns None if the image cannot be opened. It is strange that this does not cause an exception.

 import cv2 import os def load_images_from_folder(folder): images = [] for filename in os.listdir(folder): img = cv2.imread(os.path.join(folder,filename)) if img is not None: images.append(img) return images 
+8
source
 import glob cv_img = [] for img in glob.glob("Path/to/dir/*.jpg"): n= cv2.imread(img) cv_img.append(n)` 
+6
source

I used skimage. You can create a collection and access elements in a standard way, that is, col [index]. This will give you RGB values.

 from skimage.io import imread_collection #your path col_dir = 'cats/*.jpg' #creating a collection with the available images col = imread_collection(col_dir) 
+5
source

you can use the glob function for this. see example

 import cv2 import glob for img in glob.glob("path/to/folder/*.png"): cv_img = cv2.imread(img) 
+3
source

You are also using matplotlib , try this:

 import matplotlib.image as mpimg def load_images(folder): images = [] for filename in os.listdir(folder): img = mpimg.imread(os.path.join(folder, filename)) if img is not None: images.append(img) return images 
+1
source

Source: https://habr.com/ru/post/987179/


All Articles