-2

I have 10k images in one folder. In that, I have images in the order, 0.jpg, 1.jpg, 2.jpg to 10000.jpg. I have to read the images in the same order, process them, and store them in the same order. I have used the below code. But I can't get the expected output. Randomly it is reading files from the folder so, the output folder is randomly ordered. What should I change?

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

  • 2
    This question is not clear. Inheretly files have no "order". There is only a "display order", which might be by date, size, name, etc etc. In any case this "order" is only visual. If you want to view the files in another order, you should do it in the dir view settings in your OS. There is nothing that your Python code can do about that – DeepSpace Jul 05 '20 at 14:44
  • https://stackoverflow.com/a/33159707/2836621 – Mark Setchell Jul 05 '20 at 14:45
  • @MarkSetchell this is not what OP is asking – DeepSpace Jul 05 '20 at 14:52
  • sort the files in the directory (i.e. sort the result of os.listdir(path)) any the way you want, then operate on it. – Patrick Artner Jul 05 '20 at 15:12
  • @DeepSpace I understood OP wants to process 0.jpg, then 1.jpg, then 2.jpg and was suggesting they use `sort` to sort the listing regardless of how the OS chooses to present them. What do you feel OP wants please? – Mark Setchell Jul 05 '20 at 15:23

1 Answers1

1

The updated code is:

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

I'm assuming all the images in the directory are .jpg images. I have just extracted the file no for all files in the directory and sorted them as integers. Hope this helps

Aashish A
  • 111
  • 3