Reading multiple images in a folder in OpenCv (python)

I want to read multiple images in one folder using opencv (python). For this I need to use a loop foror a loop whilewith imreadfuncion? If so, how? please help me...

I want to get the images into an array and then process them one at a time in a loop.

+4
source share
2 answers

This will get all the files in the folder in onlyfiles. And then he will read them all and save them in an array images.

from os import listdir
from os.path import isfile, join
import numpy
import cv2

mypath='/path/to/folder'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
  images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
+7
source
import glob

images = [cv2.imread(file) for file in glob.glob("path/to/files/*.png")]
+6
source

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


All Articles