Python 使用给定文件夹中的 imread 加载所有图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30230592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading all images using imread from a given folder
提问by user107986
Loading and saving images in OpenCV is quite limited, so... what is the preferred ways to load all images from a given folder? Should I search for files in that folder with .png or .jpg extensions, store the names and use imread
with every file? Or is there a better way?
在 OpenCV 中加载和保存图像非常有限,所以......从给定文件夹加载所有图像的首选方法是什么?我应该在该文件夹中搜索扩展名为 .png 或 .jpg 的文件,存储名称并imread
与每个文件一起使用吗?或者,还有更好的方法?
回答by derricw
Why not just try loading all the files in the folder? If OpenCV can't open it, oh well. Move on to the next. cv2.imread()
returns None
if the image can't be opened. Kind of weird that it doesn't raise an exception.
为什么不尝试加载文件夹中的所有文件?如果 OpenCV 不能打开它,哦好吧。继续下一个。 如果图像无法打开,则cv2.imread()
返回None
。有点奇怪,它不会引发异常。
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
回答by open source guy
you can use glob function to do this. see the example
你可以使用 glob 函数来做到这一点。看例子
import cv2
import glob
for img in glob.glob("path/to/folder/*.png"):
cv_img = cv2.imread(img)
回答by A.Feliz
import glob
cv_img = []
for img in glob.glob("Path/to/dir/*.jpg"):
n= cv2.imread(img)
cv_img.append(n)`
回答by Mariyan Zarev
I used skimage. You can create a collection and access elements the standard way, i.e. col[index]. This will give you the RGB values.
我用过skimage。您可以创建一个集合并以标准方式访问元素,即 col[index]。这将为您提供 RGB 值。
from skimage.io import imread_collection
#your path
col_dir = 'cats/*.jpg'
#creating a collection with the available images
col = imread_collection(col_dir)
回答by Rishabh Agrahari
You can also use matplotlibfor this, try this out:
您也可以为此使用matplotlib,试试这个:
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
回答by Cassidy S.
To add onto the answer from Rishabh and make it able to handle files that are not images that are found in the folder.
添加到 Rishabh 的答案并使其能够处理不是在文件夹中找到的图像的文件。
import matplotlib.image as mpimg
images = []
folder = './your/folder/'
for filename in os.listdir(folder):
try:
img = mpimg.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
except:
print('Cant import ' + filename)
images = np.asarray(images)
回答by Nirmal
If all images are of the same format:
如果所有图像的格式相同:
import cv2
import glob
images = [cv2.imread(file) for file in glob.glob('path/to/files/*.jpg')]
For reading images of different formats:
用于读取不同格式的图像:
import cv2
import glob
imdir = 'path/to/files/'
ext = ['png', 'jpg', 'gif'] # Add image formats here
files = []
[files.extend(glob.glob(imdir + '*.' + e)) for e in ext]
images = [cv2.imread(file) for file in files]
回答by Akshay Salvi
import os
import cv2
rootdir = "directory path"
for subdir, dirs, files in os.walk(rootdir):
for file in files:
frame = cv2.imread(os.path.join(subdir, file))