【问题标题】:Read images from from the folder in the same order in which they appears and store in the same order以它们出现的相同顺序从文件夹中读取图像并以相同的顺序存储
【发布时间】:2020-07-05 14:41:01
【问题描述】:

我在一个文件夹中有 10k 张图像。在那,我有图片的顺序,0.jpg,1.jpg,2.jpg到10000.jpg。我必须以相同的顺序读取图像,处理它们,并以相同的顺序存储它们。我使用了下面的代码。但我无法获得预期的输出。随机从文件夹中读取文件,因此输出文件夹是随机排序的。我应该改变什么?

path = "location of the image folder"
i=0
for fname in os.listdir(path):    
    img = imageio.imread(path + '/' + fname)
    img_aug = seq.augment_image(img) # perform imgaug library data augmentation
    imageio.imwrite(os.path.join(path, path+ '/output/'+ "%1d.jpg" % (i,)), img_aug)
    print(i)
    i += 1

【问题讨论】:

  • 这个问题不清楚。 Inheretly 文件没有“顺序”。只有一个“显示顺序”,可能是日期、大小、名称等。无论如何,这个“顺序”只是视觉上的。如果您想以其他顺序查看文件,您应该在操作系统的目录视图设置中进行。你的 Python 代码对此无能为力
  • @MarkSetchell 这不是 OP 的要求
  • 对目录中的文件进行排序(即对os.listdir(path)的结果进行排序)任意你想要的方式,然后对其进行操作。
  • @DeepSpace 我知道 OP 想要处理 0.jpg,然后是 1.jpg,然后是 2.jpg,并且建议他们使用 sort 对列表进行排序,无论操作系统如何选择呈现它们.请问你觉得OP想要什么?

标签: python python-3.x image python-2.7 storing-data


【解决方案1】:

更新后的代码是:

path = "location of the image folder"
i=0
list_dir=[int(file.split(".")[0]) for file in os.listdir(path)]
list_dir.sort()
for fname in list_dir:    
    img = imageio.imread(path + '/' + str(fname)+".jpg")
    img_aug = seq.augment_image(img) # perform imgaug library data augmentation
    imageio.imwrite(os.path.join(path, path+ '/output/'+ "%1d.jpg" % (i,)), img_aug)
    print(i)
    i += 1

我假设目录中的所有图像都是 .jpg 图像。我刚刚为目录中的所有文件提取了文件 no 并将它们排序为整数。希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2014-01-06
    • 2020-06-07
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多