0

I have a code for finding same images in a folder and delete them. but I get "cannot identify image file" in some folders. how can I fix this?

def printDifferences(folder1, folder2):
    matchFound = False
    now = datetime.datetime.now()
    destFolder = "{}{}{}-{}-{}".format(now.year, now.month, now.day, 
now.second, now.microsecond)
    matchCount = 0
    if folder2 == None:
        for f1 in folder1:
            for f2 in folder1:
                if f1[1] == f2[1] and f1[0] != f2[0]:                                                           
                    matchCount += 1                  

                    print("{ Match found: ")
                    print("\t" + f1[0])
                    print("\t" + f2[0])
                    print("}")
                    a= f2[0]     
                    if (os.path.exists(a) and matchFound == True and 
matchCount%2==0):

                        try: 
                            os.remove(a)
                            print("deleted " + a)
                        except: pass                                              
    else:
        for f1 in folder1:
            for f2 in folder2:
                if f1[1] == f2[1]:
                    matchCount += 1
                    matchFound = True
                    processMatchedImages(f1, f2, matchCount, destFolder)

    if not matchFound:
        print("No matches found!")

def getOnlyFilename(fullpath):
    return fullpath.split("\\")[-1]

def getAllImageHashes(folder):
    onlyfiles = [join(folder, f) for f in listdir(folder) if 
isfile(join(folder, f)) and not f.endswith(".ini") and not 
f.endswith(".db")]
    hashedFiles = []
    fileLength = len(onlyfiles)
    for f in onlyfiles:
        hashedFiles.append((f, dhash(Image.open(f))))
    print("Hashed all files from folder: "+ folder)
    return hashedFiles

    pixels = list(image.getdata())
    difference = []
    for row in range(hash_size):
        for col in range(hash_size):
            pixel_left = image.getpixel((col, row))
            pixel_right = image.getpixel((col + 1, row))
            difference.append(pixel_left > pixel_right)

    decimal_value = 0
    hex_string = []
    for index, value in enumerate(difference):
        if value:
            decimal_value += 2**(index % 8)
        if (index % 8) == 7:
            hex_string.append(hex(decimal_value)[2:].rjust(2, '0'))
            decimal_value = 0

        return ''.join(hex_string)

in some images folders I face with this error: OSError: cannot identify image file 'C:\Users\MIna\Desktop\5\5\12.jpg' can you tell me how I should fix it?

mina
  • 1
  • 2
  • 5
    Welcome to SO. When asking a question, please try to be as specific as you can about your problem and provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your code [within your question](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – trotta Aug 09 '19 at 11:57
  • Please include any relevant code :) – KuboMD Aug 09 '19 at 11:58
  • is it related to [this](https://stackoverflow.com/questions/19230991/image-open-cannot-identify-image-file-python) question? – Alex B Aug 09 '19 at 12:01
  • thanks for answering...but I cannot fix my problem by this link. I installed pillow and upgrade it before – mina Aug 09 '19 at 12:19

0 Answers0