Python 如何循环目录中的图像?
目录中有cats图像。我想手动将数据增强应用于这些图像 我的目录是Python 如何循环目录中的图像?,python,computer-vision,Python,Computer Vision,目录中有cats图像。我想手动将数据增强应用于这些图像 我的目录是下载/Images/Cats/ 此目录包含两种类型的标签/名称的图像: cat1_001.png,cat1_002.png cat2_001.png,cat2_002.png 指定两种类型的cat图像 那么,如何仅从python中的目录迭代标签为1的图像呢?您可以这样做: import os directory_in_str = 'Downloads/Images/Cats/' directory = os.fsencode(di
下载/Images/Cats/
此目录包含两种类型的标签/名称的图像:
cat1_001.png
,cat1_002.png
cat2_001.png
,cat2_002.png
那么,如何仅从python中的目录迭代标签为1的图像呢?您可以这样做:
import os
directory_in_str = 'Downloads/Images/Cats/'
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
if(filename.startswith('cat1_') and filename.endswith('.png')):
print(filename)
# do your staff here
glob.glob('Downloads/Images/Cats/*001.png')中的f的
:…
或者可能cat1.*.png
,具体取决于“标签1”的含义。当一个描述可能模棱两可时,在问题中加入一个例子通常是有帮助的。我将测试文件名是否以cat1开头,而不是测试cat1是否在文件名中。我们不知道名称约定,这应该更准确。好的观点:)我编辑了anwser