【问题标题】:Python3 create list of image in a folderPython3在文件夹中创建图像列表
【发布时间】:2019-08-11 15:13:59
【问题描述】:

我在 Python3 中有一个像这样的图像数组...

images = [
    "/var/www/html/myfolder/images/1.jpg",
    "/var/www/html/myfolder/images/441.jpg",
    "/var/www/html/myfolder/images/15.jpg",
    "/var/www/html/myfolder/images/78.jpg",
]

我不想像这样指定图像,而是想传递一个绝对路径并让 python 从该路径中的 .jpg 图像中创建图像列表。

我最好的方法是什么?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

你可以使用 glob。

glob.glob(pathname, *.jpg, recursive=False)

返回与路径名匹配的可能为空的路径名列表,该列表必须是包含路径规范的字符串。路径名可以 可以是绝对的(如 /usr/src/Python-1.5/Makefile)或相对的 (如 ../../Tools//.gif),并且可以包含 shell 样式的通配符。 结果中包含损坏的符号链接(如在 shell 中)。

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is

后跟 os.sep,只有目录和子目录匹配。

假设你的 abs 路径是 myfolder

import glob
images = glob.glob('images/*.jpg')

https://docs.python.org/3/library/glob.html#glob.glob

【讨论】:

    【解决方案2】:

    Python 3 中的pathlib 模块让这一切变得简单:

    from pathlib import Path
    images = Path("/var/www/html/myfolder/images").glob("*.jpg")
    

    是否希望所有 jpg 图像都递归地放在该目录下?使用.glob("*/**.jpg")

    请注意,这是创建一个 Path 对象数组。如果你想要字符串,只需转换它们:

    image_strings = [str(p) for p in images]
    

    【讨论】:

    • 它工作正常,也解决了数据类型错误。
    【解决方案3】:

    如果您指定路径,则有多种方法可以找到该目录中的所有文件。获得该列表后,您可以简单地遍历它并创建图像。

    见:How do I list all files of a directory?

    一个很好的方法是使用 os.listdir

    import os
    
    # specify the img directory path
    path = "path/to/img/folder/"
    
    # list files in img directory
    files = os.listdir(path)
    
    for file in files:
        # make sure file is an image
        if file.endswith(('.jpg', '.png', 'jpeg')):
            img_path = path + file
    
            # load file as image...
    

    【讨论】:

    • 感谢您发布此信息。这是非常有帮助的。我确实注意到一个错字。您忘记了 '.png. 之后的单引号
    【解决方案4】:

    只扫描顶层

    import os
    path = "path/to/img/folder/"
    jpgs = [os.path.join(path, file)
            for file in os.listdir(path)
            if file.endswith('.jpg')]
    

    要递归扫描,请将最后一行替换为

    jpgs = [os.path.join(root, file)
            for root, dirs, files in os.walk(path)
            for file in files
            if file.endswith('.jpg')]
    

    【讨论】:

      【解决方案5】:
      import os
      
      images=[]
      
      def getFiles(path):
          for file in os.listdir(path):
              if file.endswith(".jpg"):
                  images.append(os.path.join(path, file))
      

      图片列表:

      filesPath = "/var/www/html/myfolder/images"
      
      getFiles(filesPath)
      print(images)
      

      【讨论】:

        【解决方案6】:
        • 现代方法是使用 pathlib哪个 将路径视为对象,而不是字符串。作为一个对象,那么所有路径 具有访问路径的各种组件的方法,(例如 .suffix, .stem)。
        • pathlib 还有:
          • .glob 内置
          • .open 方法(例如Path.open(mode='r')
        • Python 3's pathlib Module: Taming the File System

        代码:

        from pathlib import Path
        
        jpg_files = Path('/some_path').glob('*.jpg')
        
        for file in jpg_files:
            with file.open(mode='r') as f:
                ...
                do some stuff
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-01
          相关资源
          最近更新 更多