【问题标题】:How do I sort a list of images alphanumerically in python? [duplicate]如何在python中按字母数字对图像列表进行排序? [复制]
【发布时间】:2020-02-29 17:32:58
【问题描述】:

我有一个包含一堆图像的文件夹,我将它们转换为 16:9,然后将它们导出到另一个文件夹中,一切正常,除了导入图像时它们没有存储排序含义,导出时它们完全处于顺序不同,怎么办?

for filepath in glob.iglob(folderToEdit + '\*.jpg'):
imageList.append(filepath)

【问题讨论】:

标签: python


【解决方案1】:

您可以在检索文件名后对它们进行排序:

for filepath in sorted(glob.iglob(folderToEdit + '\*.jpg')):
    imageList.append(filepath)

此外,如果您想忽略大小写,可以通过将 key 参数指定为这些文件名的小写版本来实现:

for filepath in sorted(glob.iglob(folderToEdit + '\*.jpg'), key=lambda x: x.lower()):
    imageList.append(filepath)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 2020-03-12
  • 2018-09-03
相关资源
最近更新 更多