gpt4 book ai didi

Python3在文件夹中创建图像列表

转载 作者:行者123 更新时间:2023-12-05 08:16:12 24 4
gpt4 key购买 nike

我在 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 图像中为我创建图像列表。

我最好的方法是什么?

最佳答案

您可以使用 glob。

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

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

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

followed by an os.sep, only directories and subdirectories match.

假设您的 abs 路径是 myfolder

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

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

关于Python3在文件夹中创建图像列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com