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]) )
# This is to get the names of all the files in the desired directory
# Here I assume that they are all images
original_images = os.listdir('./path/containing/images')
# Here I construct a list of relative path strings for each image
original_images = [f"./path/containing/images/{file_name}" for file_name in original_images]
original_images = [cv2.imread(file) for file in original_images]
def flatten_images(folder): # Path of folder (dataset)
images=[] # list contatining all images
for filename in os.listdir(folder):
print(filename)
img=plt.imread(folder+filename) # reading image (Folder path and image name )
img=np.array(img) #
img=img.flatten() # Flatten image
images.append(img) # Appending all images in 'images' list
return(images)
import glob
import cv2
path="D:\OMR_IMAGES\*.jpg" #Replace with your folder
path("your path\*.jpg")
k=glob.glob(path)
images=[cv2.imread(images) for images in glob.glob(path)]
print(len(images)) #number of images in folder
for i in range(len(images)):
cv2.imshow("images",images[i])
cv2.waitKey(0)
8条答案
按热度按时间pbpqsu0x1#
gv8xihay2#
这将获取
onlyfiles
中一个文件夹中的所有文件,然后读取它们并将它们存储在数组images
中。kmb7vmvb3#
lrl1mhuk4#
这个有更好的时间效率。
mbskvtky5#
nzrxty8p6#
下面是我在不使用
glob
的情况下,使用os
模块的方法,因为我无法让它在我的计算机上与glob
一起工作:e7arh2l67#
s4n0splo8#
你可以试试这个
在我的情况下,它起作用了