0

I have written the following code for a program to display the image path names in a folder. However, as seen in the output, the pictures' paths are not being displayed in the correct order despite me using sorted. How can I display them in their sorted order?

Code:

import os
import sys
from PIL import Image
import PIL.ImageOps
import glob

path="/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/"
print(path)
files=sorted(glob.glob(path+"*.JPG"))
#print(files)
file_index=0
for f in files:
    file_index=file_index+1
    print(f,file_index)

Output:

/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/1.JPG 1
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/10.JPG 2
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/11.JPG 3
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/12.JPG 4
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/13.JPG 5
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/14.JPG 6
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/2.JPG 7
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/3.JPG 8
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/4.JPG 9
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/5.JPG 10
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/6.JPG 11
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/7.JPG 12
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/8.JPG 13
/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/9.JPG 14
Sibi
  • 2,221
  • 6
  • 24
  • 37
  • It is because they are sorted as `strings`. Try sorting them using an `int` cast name of the file. i.e. if it is `file=path/to/file/7.jpg` try something like, `n=int(file.split('/')[-1].split('.jpg'))` and then sort using `n` – SvbZ3r0 Jun 10 '16 at 05:34
  • @GughanRavikumar Could you please elaborate? How can we sort using`n`. I have understood why you are doing this but am not sure how to implement it. Thanks! – Sibi Jun 10 '16 at 05:41

2 Answers2

3

This should work, assuming the file names are numbers.

import os
import sys
from PIL import Image
import PIL.ImageOps
import glob

path = "/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/"
print(path)
files = glob.glob(path+"*.JPG")
n = [int(i) for i in map(lambda x: x.split('/')[-1].split('.jpg')[0], files)]
files = [x for (y, x) in sorted(zip(n, files))]
print(files)

Essentially, your code isn't working because you sorted the files as strings.

SvbZ3r0
  • 638
  • 4
  • 19
  • Error: `TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' ` – Sibi Jun 10 '16 at 06:27
  • It works fine for me. `int(i)` gets `i` as an argument which is a `str` and not a `list`. Try `lambda x: x.split('/')[-1].split('.jpg')[0]` just to be sure. Your file name might have spaces following the extension. – SvbZ3r0 Jun 10 '16 at 06:37
  • Yeah it worked. What was the problem? – Sibi Jun 10 '16 at 06:50
  • 1
    Your file name might have had spaces after the extension. Thus, `split('.jpg')` would have returned `['7',' ']`, which is a list. Come to think of it, the second method would me more appropriate. I'll update the answer. – SvbZ3r0 Jun 10 '16 at 06:55
2

As others have stated, you are comparing strings. You could create a custom comparator for this:

files = glob.glob(path+"*.JPG")
sortedfiles = sorted(files, key=lambda filepath: int(filepath.split('/')[-1].split('.jpg')))

Whole snippet:

import os
import sys
from PIL import Image
import PIL.ImageOps
import glob

path="/home/srilatha/Desktop/Research_intern/Data_sets/Expanded_data_set/1/"
print(path)
files=glob.glob(path+"*.JPG")
sortedfiles = sorted(files, key=lambda filepath: int(filepath.split('/')[-1].split('.jpg')[0]))

for f in sortedfiles:
    file_index=file_index+1
    print(f,file_index)
user3636636
  • 2,409
  • 2
  • 16
  • 31