pythonpython-imaging-librarynonetypephash

phash breaks when nonetype object apears


import os, cv2, imagehash, PIL



hashList = []

path = "C:\cardscryer\CardScryer-master\pictures"

folderList = os.listdir(path)
print(folderList)

os.chdir(path)

for folder in folderList:

    fileList = os.listdir(folder)

    for file in fileList:

        cardImage = cv2.imread(os.path.join(folder, file))

        cropImage = cardImage[15:200, 20:420]

        hash = imagehash.phash(PIL.Image.fromarray(cropImage))

        file = file.replace('.jpg','')

        print(file)

        hashList.append([file[:-4], folder, hash])

I think "cv2.imread(os.path.join(folder, file))" is returning as none for some reason because the next assignment gives a nonetype error but it only breaks when all the sub folders are running. Changing it to run on only the sub folder that the error happens in; it runs fine. I'm most baffled by the returns being different on such a minor change.


Solution

  • When using backslashes in Windows pathnames, either escape them or use raw strings:

    path = "C:\\cardscryer\\CardScryer-master\\pictures"
    

    or

    path = r"C:\cardscryer\CardScryer-master\pictures"