以图像出现的相同顺序从文件夹中读取图像并以相同的顺序存储
Read images from from the folder in the same order in which they appears and store in the same order
我在一个文件夹中有 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 图像。我刚刚提取了目录中所有文件的文件号,并将它们排序为整数。希望这有帮助
我在一个文件夹中有 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 图像。我刚刚提取了目录中所有文件的文件号,并将它们排序为整数。希望这有帮助