如何修复python中的“无法识别图像文件”错误

时间:2019-08-09 11:55:26

标签: python

我有一个代码可以在文件夹中查找相同的图像并将其删除。 但在某些文件夹中出现“无法识别图像文件”。 我该如何解决?

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)

在一些我遇到此错误的图像文件夹中: OSError:无法识别图像文件'C:\ Users \ MIna \ Desktop \ 5 \ 5 \ 12.jpg' 你能告诉我该怎么解决吗?

0 个答案:

没有答案