hulkinBrain hulkinBrain - 1 year ago 84
Python Question

Error while reading image using cv2.imread() in Django

I am trying to apply image processing on an image which i'm loading through

in Django's
file, but i'm getting an

Following is the hierarchy

Heirarchy through which i'm trying to read

def process_image(request):
url = "static/images/temp321.jpg"
a = cv2.imread(url)
r, c = a.shape

Error which i'm getting:

Error message snapshot

What am i doing wrong?

Answer Source

This error indicates that a is None. With a being the result of the call to cv2.imread

If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix

This is from the cv2 c documentation. This translates to None in python. So the problem essentially is that your file is not being read.

The solution: If you have setup the PROJECT_ROOT variable properly in your file

os.path.join(PROJECT_ROOT, "static/images/temp321.jpg")

If you don't have PROJECT_ROOT setup, add to

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

Note that you may still have trouble if you are doing this in production because the webserver may not be allowed to read from that directory. In that case you will need to change file system permissions

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download