Search code examples
pythonlistimage-comparison

work with image: imgcompare does not support list?


import imgcompare
...

for filename in os.listdir(myPath):
     if filename.endswith(".png"):
         listIm1.append(filename)

for filename2 in os.listdir(myPath2):
     if filename2.endswith(".png"):
         listIm2.append(filename2)

so i fill my two lists with images,now I would like to compare the images of the two lists one by one following the same index, for example:
listIm1[0] with listImg2[0]
listIm1[1] with listImg2[1]
and so on... and that's the code:

for item in listIm1:
        ifSame = imgcompare.is_equal(listIm1[item],listIm2[item],tolerance=2)
        print ifSame

but get the error:

same = imgcompare.is_equal(listIm1[item], listIm2[item], tolerance=2)
TypeError: list indices must be integers, not str

it seems that imgcompare.is_equal() does not work with lists, is there some pythonic expedient to make it works?


Solution

  • since

     if filename2.endswith(".png"):
             listIm2.append(filename2)
    
    for item in listIm1:
            # item = "someimagine.png"
     ifSame = imgcompare.is_equal(listIm1[item],listIm2[item],tolerance=2)
            #listIm1[someimagine.png] is what you are asking => retrun Type Error
    

    I guess you are looking for something like this:

    edit:

    import os
    
    for filename in os.listdir(myPath):
        if filename2.endswith(".png"):
           img_path = os.path.join(myPath,filename2)  
           listIm2.append(img_path)
    
    listIm1 = [] 
    listIm2 = []
    for i in range(len(listIm1)):
    
         ifSame = imgcompare.is_equal(listIm1[i],listIm2[i],tolerance=2)
         print ifSame
    

    and it's better if len(listIm1) == len(listIm2)