Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何循环目录中的图像?_Python_Computer Vision - Fatal编程技术网

Python 如何循环目录中的图像?

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

目录中有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(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