Last updated: Apr 8, 2024
Reading timeยท3 min
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.
Here is a very simple example of how the error occurs.
import cv2 # ๐๏ธ None img = cv2.imread('bad-path.png') # โ๏ธ AttributeError: 'NoneType' object has no attribute 'shape' print(img.shape)
The imread
method returns None when passed an
incorrect path.
Trying to access the shape
attribute on a None
value causes the error.
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.
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')
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.
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')
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.
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.
try/except
statement to handle the errorYou can also use a try/except statement to handle the error.
import cv2 img = cv2.imread('thumbnail.webp') try: print(img.shape) except AttributeError: print('AttributeError: value is', img)
If accessing the shape
attribute on the image causes an AttributeError
, the
except
block runs.
cv2.imread()
You can pass a path to the os.path.exists()
method to check if the path
exists.
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
The os.getcwd()
method returns the current working directory and can be used
when constructing a path.
None
valuesThe most common sources of None
values are:
None
implicitly).None
.Note that all functions that don't explicitly return a value, implicitly return
None
.
Here is another example of how the error occurs.
import cv2 def get_path(): print('thumbnail.webp') # ๐๏ธ None img = cv2.imread(get_path()) # โ๏ธ AttributeError: 'NoneType' object has no attribute 'shape' print(img.shape)
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.
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.
Make sure you haven't reassigned a variable that stores a string to None
by
mistake.
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.
sort()
and append()
) return None
.If the error persists, follow the instructions in my AttributeError: 'NoneType' object has no attribute 'X' article.
You can learn more about the related topics by checking out the following tutorials: