Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to preserve order in python while comparing images from 2 different directories?

I was comparing images from 2 different directories. I write code but the sequence of comparing is this

{ 0,1,10,11,12,…,19,2,20,21,..} not like {0,1,2,3,…,9,10,11,12,…}

L1 = os.listdir("D:\\image_dir_1\\")

image_list_1 = list()
image_list_2 = list()
final_i = list()
final_j = list()
dirs = -1 

for i in L1:
    dirs = dirs + 1
    img1 = cv2.imread("D:\\image_dir_1\\" +i )
    L2 = os.listdir("D:\\image_dir_2\\"+str(dirs) +'\\')
    for j in L2:
        img2 = cv2.imread("D:\\image_dir_2\\" + str(dirs)+ '\\' +j )
        image_list_1.append(FUN_1(img1 , img2))
        image_list_2.append(FUN_2(img1 , img2))
        final_i.append(i)
        final_j.append(j)

    filei = pd.DataFrame(final_i,columns=['Col_1'])
    filej = pd.DataFrame(final_j,columns=['Col_2'])

    frame_1 = pd.DataFrame(image_list_1,columns=['c1'])
    frame_2 = pd.DataFrame(Image_list_2,columns=['c2'])
    
    final_value = pd.concat([filei, filej, frame_1,frame_2],axis=1)

final_value.to_csv('spreadsheet.csv',index=None)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to sort the L1 list:

L1 = sorted(os.listdir("D:\\image_dir_1\\"), key=lambda str: int(str[:-4].strip()))

The issue is that without sorting them by their actual int value, you have the files names sorted lexicographically.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading