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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

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

The Python "AttributeError: 'NoneType' object has no attribute 'shape'" occurs when we access the shape attribute on a None value, e.g. after passing an incorrect path to cv2.imread().

To solve the error, make sure to specify the correct path.

attributeerror nonetype object has no attribute shape

Here is a very simple example of how the error occurs.

main.py
import cv2 # ๐Ÿ‘‡๏ธ None img = cv2.imread('bad-path.png') # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'shape' print(img.shape)

nonetype object has no attribute shape

The imread method returns None when passed an incorrect path.

Trying to access the shape attribute on a None value causes the error.

# Checking if the variable is not None before accessing shape

You can use an if statement to check if the variable is not None before accessing the shape attribute but you'd still have to correct the path.

main.py
import cv2 img = cv2.imread('thumbnail.webp') if img is not None: print('variable is not None') print(img.shape) else: print('variable is None')

check if variable is not none before accessing shape

The if block is only run if the img variable doesn't store a None value, otherwise, the else block runs.

You can also pass an absolute path to the cv2.imread() method.

main.py
import cv2 img = cv2.imread(r'/home/borislav/Desktop/bobbyhadz_python/thumbnail.webp') if img is not None: print('variable is not None') print(img.shape) else: print('variable is None')

using an absolute path instead

I'm on Linux, so my absolute path starts with /home/user.

If you are on Windows, your absolute path will look something similar to the following.

main.py
import cv2 img = cv2.imread(r'C:\Users\bobby_hadz\Desktop\thumbnail.webp') if img is not None: print('variable is not None') print(img.shape) else: print('variable is None')

Make sure to prefix the path with an r to have it treated as a raw string.

# Using a try/except statement to handle the error

You can also use a try/except statement to handle the error.

main.py
import cv2 img = cv2.imread('thumbnail.webp') try: print(img.shape) except AttributeError: print('AttributeError: value is', img)

using try except statement to handle the error

If accessing the shape attribute on the image causes an AttributeError, the except block runs.

# Checking if a path exists before calling cv2.imread()

You can pass a path to the os.path.exists() method to check if the path exists.

main.py
import os # ๐Ÿ‘‡๏ธ Check if the path exists print(os.path.exists('thumbnail.webp')) # ๐Ÿ‘‡๏ธ Returns the current working directory print(os.getcwd()) # ๐Ÿ‘‰๏ธ /home/borislav/Desktop/bobbyhadz_python

checking if the path exists

The os.getcwd() method returns the current working directory and can be used when constructing a path.

# Common sources of None values

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Note that all functions that don't explicitly return a value, implicitly return None.

# A function that doesn't return anything returns None

Here is another example of how the error occurs.

main.py
import cv2 def get_path(): print('thumbnail.webp') # ๐Ÿ‘‡๏ธ None img = cv2.imread(get_path()) # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'shape' print(img.shape)

function returning none

The get_path function doesn't return anything, so it returns None.

We ended up passing None to the cv2.imread method, so the error occurred when we accessed the shape property on a None value.

To solve the error, make sure to return a value from the function.

main.py
import cv2 def get_path(): return 'thumbnail.webp' # ๐Ÿ‘‡๏ธ None img = cv2.imread(get_path()) print(img.shape) # ๐Ÿ‘‰๏ธ (120, 632, 3)

We used the return statement to return a string from the get_path function, so everything works as expected.

You might also get a None value from a function if the function returns a value only if a certain condition is met.

You have to make sure to return a value from the function in all cases.

Otherwise, the function will return None if the given condition is not met.

# Reassigning a variable to None by mistake

Make sure you haven't reassigned a variable that stores a string to None by mistake.

main.py
import cv2 path = 'thumbnail.webp' # ๐Ÿ‘‡๏ธ Reassign to None by mistake path = None img = cv2.imread(path) # โ›”๏ธ AttributeError: 'NoneType' object has no attribute 'shape' print(img.shape)

We initially set the path variable to a string but later reassigned it to a None value which caused the error.

In this case, you have to track down where the variable got set to None and correct the assignment.

Note that many built-in methods that mutate an object (e.g. sort() and append()) return None.

If the error persists, follow the instructions in my AttributeError: 'NoneType' object has no attribute 'X' article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev