Python 从文件夹中以相同的显示顺序读取图像,并以相同的顺序存储

Python 从文件夹中以相同的显示顺序读取图像,并以相同的顺序存储,python,python-3.x,image,python-2.7,storing-data,Python,Python 3.x,Image,Python 2.7,Storing Data,我在一个文件夹中有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.

我在一个文件夹中有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

更新后的代码为:

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图像。我刚刚提取了目录中所有文件的文件号,并将它们作为整数排序。希望这有帮助

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