0

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?

3 Answers3

1

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)

Peko Chan
  • 306
  • 5
  • 15
  • same error as above: `IOError: [Errno 2] No such file or directory: 'image.png'`but with `for i in range(len(listIm1)): print listIm1[i]` file is found. – mario Letterman Apr 17 '18 at 22:17
  • I got it, first you have to declare your list in order to append images to them, using `listIm1 = []` and `listIm1 = []`, I have updated the answer – Peko Chan Apr 18 '18 at 05:28
  • Done, but got the same error `No such file or directory: 'image.png'` even if the file still exists. – mario Letterman Apr 18 '18 at 07:20
  • ok, now you might need to declare the full path if the images are not in the same directory, somethings like `import os` `os.path.join("your path directory",listIm1[i])` if you print you'll get"c:\yourpath\image.png" and it should work, I've edit the anwser above – Peko Chan Apr 18 '18 at 07:56
  • I was missing a basic instruction **os.chdir(mypath)**, so your is the correct answer. – mario Letterman Apr 19 '18 at 22:38
1

The problem here is that you are trying to get the index of listIm1 by using item. What you want to do is use a range(), like:

for i in range(len(listIm1)):
            ifSame = imgcompare.is_equal(listIm1[i],listIm2[i],tolerance=2)

As @Matt pointed out, this will only work if you know the lists are the same length beforehand, otherwise it will throw an index error.

Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80
  • 1
    Careful! This assumes that listIm1 and listIm2 are the same length! – Matt Apr 17 '18 at 21:28
  • Yep, that's right. Will update the question to reflect this. – Colin Ricardo Apr 17 '18 at 21:30
  • @colin with your code have error: `IOError: [Errno 2] No such file or directory: 'image.png'`. But with `for i in range(len(listIm1)): print listIm1[i]`have: image.png, image2.png,... – mario Letterman Apr 17 '18 at 21:53
  • imgcompare is trying to find a file named 'image.png'. If you are getting that error, that means that there isn't a file named image.png in your current directory. When you are appending to your lists, try using `listIm1.append(os.path.abspath(filename))` and `listIm2.append(os.path.abspath(filename))` instead – Matt Apr 17 '18 at 21:54
  • It's quite large, a specific section? – mario Letterman Apr 17 '18 at 21:59
  • `listIm1.append(os.path.abspath(filename))` unfortunately the same error but with the full path: `IOError: [Errno 2] No such file or directory: 'C:\\Users\\a\\Documents\\PycharmProjects\\arduino\\image.png'` – mario Letterman Apr 17 '18 at 22:07
  • What is `myPath` and `myPath2`? – Colin Ricardo Apr 17 '18 at 22:08
0

You are using a for each loop, which grabs each element in your provided list listIm1 and stores it in a temp variable item, you then pass item (which is a string) as an index both of your lists. Indices of a list must be an integer, and that is the error you are getting.

for dir1_file in listIm1:
    for dir2_file in listIm2:
        ifSame = imgcompare.is_equal(dir1_file,dir2_file,tolerance=2)
        print ifSame

This code uses two for each loops, it looks at each element in both of the lists and uses them as the arguments for your method.

Matt
  • 973
  • 8
  • 10
  • I would like to compare images in two lists with same index, this compare the first index of first list with all index of second list and so on, right? – mario Letterman Apr 17 '18 at 22:15
  • 1
    You are correct. This will compare every file in dir1 with every file in dir2. If you want to compare two lists using the same index, I would suggest using the answer provided by @colin. You just have to be sure that your two lists are going to be the same size, and be in the order you want. – Matt Apr 17 '18 at 22:26
  • I tried to use colin's answer but got an error `IOError: [Errno 2] No such file or directory: 'image.png'` even if the file exists. – mario Letterman Apr 18 '18 at 07:15