Home > Mobile >  AttributeError: 'NoneType' object has no attribute 'shape' (How to loop and comp
AttributeError: 'NoneType' object has no attribute 'shape' (How to loop and comp

Time:01-19

I am trying to compare an image with multiple images by calculating the SSIM value. I'm using the code written in the answer as reference: How do I compare SSIM between one image and many others using python?

Below is the error that I could not solve.

AttributeError: 'NoneType' object has no attribute 'shape'

I've tried resizing, and using Image.open but none worked. I read that the issue could be caused by the image not existing or is not in the right path but from the script below, the path seems to be correct but still no return.

Would like to note that the "subject" folder only has 1 image in it.

The script (left the line of code that did not worked in as comments as notes):

from skimage.metrics import structural_similarity as ssim
import matplotlib.pyplot as plt
import numpy as np
import PIL
import cv2
import os

first_dir = r'C:\Users\xx\Downloads\Sample images\subject'
second_dir = r'C:\Users\xx\Downloads\Sample images\noise'

# Loop through all files in first directory
for first_file in os.listdir(first_dir):
    f_path = os.path.join(first_dir, first_file)
    #resized_f = Image.open(f_path)
    #resized_f = resized_f.resize((687,612))
    if f_path.endswith(".png"):
        image = cv2.imread(f_path,0)
        print(image)

    # Compare each file in second directory to each file in first directory
        for second_file in os.listdir(second_dir):
            f2_path = os.path.join(second_dir, second_file)
            if f2_path.endswith(".png"):
                print(f2_path)
                #image_f = PIL.Image.open(f_path)
                #image_f2 = PIL.Image.open(f2_path)
                #resized_f2 = Image.open(f2_path)
                #resized_f2 = resized_f2.resize((687,612))
                imageA = cv2.imread(first_file, 0)
                imageB = cv2.imread(second_file, 0)
                print (imageA)
                print (imageB)
                #(score, diff) = ssim(imageA, imageB, full=True)
                #results.append((first_file, second_file, score))

and the output:

[[255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 ...
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]]

C:\Users\xx\Downloads\Sample images\noise\noise_1.png
None
None
C:\Users\xx\Downloads\Sample images\noise\noise_2.png
None
None
C:\Users\xx\Downloads\Sample images\noise\noise_3.png
None
None

I also tried adding more images in the folder with only 1 images ("subject" folder) to see if its a looping problem but that doesn't seem to be it.

Any help is appreciated. Thank you.

CodePudding user response:

So i can assume a problem in

imageA = cv2.imread(first_file, 0)
imageB = cv2.imread(second_file, 0)

You should use the full path here, so you should rewrite it to

imageA = cv2.imread(f_path, 0)
imageB = cv2.imread(f2_path, 0)

The cv2.imread(...) method does not know where your image lie.

  • Related