The Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error shows that we are trying to access the shape attribute of a None type object. None is a special type in Python that represents the absence of a value. In this short article, we will learn how we can solve Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error using various methods. Moreover, we will try to understand what the error actually means and how to read and understand Python errors.
![[Solved] AttributeError: 'nonetype' object has no attribute 'shape'](http://techfor-today.com/content_bashir/uploads/2023/03/Solved-AttributeError-nonetype-object-has-no-attribute-shape.png)
Attributeerror: ‘nonetype’ object has no attribute ‘shape’ – Possible solutions
The Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error shows that we are trying to get access to the shape attribute of the None type object which does not have any attribute. You will face this error mostly when dealing with OpenCV in Python and images. In the beginning, the error seems to be very complex but it can be easily solved.
As an example, we will take a simple example of the None type shown below:
def get_shape(x):
return x.shape
value = None
print(get_shape(value))
Output:

As you can see, we get an error that says the NoneType object has no attribute shape. In real life and coding, this people working with CV2/ OpenCV usually face this problem when they try to find out the shape of an image as shown below:
import cv2
# reading image
img = cv2.imread('image.png')
# attribute error
print(img.shape)
Output:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/tmp/ipykernel_161127/2418012391.py in <module>
5
6 # attribute error
----> 7 print(img.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
As you can see, we get the error shown above. The error can be solved by the following different methods:
- Using if-else statements
- check the path
- Using asserts keyword
- Using try-except blocks
Now we will go through each of the methods in more detail.
Solution-1: Using an if-else statement
To find out if the object is None type or not, you can use if-else statements so that even if the object is NoneTyped the problem will not be crashed:
import cv2
# reading image
img = cv2.imread('image.png')
# using if statement
if img is not None:
print(img.shape)
# else statemetn
else:
print('Object is None')
Output:
Object is None
The if statement will check if the type of the object is None type or not. If the object is NonType then the else block will execute.
Solution-2: Check the path of the object
Now, you know that the object Type is None which means that the image was not loaded correctly. There can be many possible reasons for that. One reason could be the Path of the image can be incorrect.
We can check the path of the image that we are loading using the os module.
import os
# printing the image path
print(os.path.exists('image.png'))
# printing the current directory
print(os.getcwd())
Output:
False
/home/student/Documents/Machine_Learning_Staff/Techfor-today
As you can see, the path for the image is False which means there is no such image file existing on our system. You can also check to see if the Path of the image and the python file are in the same directory or not.
If the directory of the Python file is different from the current Python file then you need to give the full path of the file.
Solution-3: Using the assert keyword
The problem with Attributeerror: ‘nonetype’ object has no attribute ‘shape’ is that it now shows where the actual error is. We can use the assert keyword and use our own message as output if the error occurs to keep track of the error.
For example, we will use the assert keyword with the image loading.
# image reading
image = cv2.imread('image.png')
# using assert method
assert not isinstance(image,type(None)), 'image not found-Please check the path or name'
Output:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
/tmp/ipykernel_161127/4142996152.py in <module>
3
4 # using assert method
----> 5 assert not isinstance(image,type(None)), 'image not found-Please check the path or name'
AssertionError: image not found-Please check the path or name
As you can see, the assert helps us to identify where the error is so that we can easily solve it.
Solution-4: Using try-except blocks
Another way to handle the Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error without crashing the script is to use the try-except block. The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.
import cv2
# reading image
img = cv2.imread('image.png')
try:
print(img.shape)
except:
print('Object is None')
Output:
Object is None
As you can see, the except block was executed because the try block gave the error:
Understanding Attributeerror: ‘nonetype’ object has no attribute ‘shape’
Let us now understand the errors in Python by taking the example of Attributeerror: ‘nonetype’ object has no attribute ‘shape’. We know that the error in Python itself helps us to understand the problem. The first part of the error gives us information about the type of error. For example, the first part of the problem says, AttributeError, which says the error that we are getting is because we are missing some attribute. Then the error gives more information about the type of the error which says None type object does not have shape attribute:
Let us now explain each part of the errors one by one:
What is an AttributeError in Python?
The Python AttributeError is an exception that occurs when an attribute reference or assignment fails. This can occur when an attempt is made to reference an attribute on a value that does not support the attribute.
For example, we know that the list object in Python has the attribute append which helps to add new values to the list. If we will use the same method on an integer value, then we will get an attribute error:
# integer variable
integer = 54
# using append method
integer.append(10)
This will return an attribute error because the integer value does not have any attribute append.
What is a None type in Python?
NoneType in Python is a data type that simply shows that an object has no value/has a value of None. You can assign the value of None to a variable but there are also methods that return None.
You can check the type of the object in Python by using the type() method as shown below:
# variables
string = "bashir"
integer = 4323
floating = 3.32
NoneType = None
# types
print(type(string))
print(type(integer))
print(type(floating))
print(type(NoneType))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'NoneType'>
Notice that we got the data type of each of the variables using the type() method.
What is an Object in Python?
An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. Python is an object-oriented programming language that stresses objects i.e. it mainly emphasizes functions.
Summary
In this short article, we learned how we can solve Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error. We discuss various different methods and solved the problem. Moreover, we also learned the reasons for getting Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error. We also learned how we can understand errors in Python taking Attributeerror: ‘nonetype’ object has no attribute ‘shape’ error as an example.
Related Issues
- [Solved] Attributeerror: module tensorflow has no attribute contrib
- [Solved] modulenotfounderror: no module named ‘matplotlib’
- AttributeError: ‘str’ object has no attribute ‘read’ [Solved]
Pingback: ValueError: Not Enough Values to Unpack [5 Cases] - Techfor-Today
Pingback: IndexError: tuple index out of range [Solved] - Techfor-Today