[英]Error while reading image using cv2.imread() in Django views.py
I am trying to apply image processing on an image which i'm loading through cv2.imread()
in Django's views.py
file, but i'm getting an AttributeError
everytime.我正在尝试对通过 Django 的
views.py
文件中的cv2.imread()
加载的图像应用图像处理,但我每次都会收到AttributeError
。
Following is the hierarchy以下是层次结构
views.py through which i'm trying to read temp321.jpg
: views.py我试图通过它阅读
temp321.jpg
:
def process_image(request):
url = "static/images/temp321.jpg"
a = cv2.imread(url)
r, c = a.shape
Error which i'm getting:我得到的错误:
What am i doing wrong?我究竟做错了什么?
This error indicates that a
is None.此错误表明
a
为无。 With a
being the result of the call to cv2.imread
a
调用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.这是来自 cv2 c 文档。 This translates to None in python.
这在 python 中转换为 None 。 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 settings.py file解决方案:如果您在 settings.py 文件中正确设置了 PROJECT_ROOT 变量
os.path.join(PROJECT_ROOT, "static/images/temp321.jpg")
If you don't have PROJECT_ROOT setup, add to settings.py如果您没有 PROJECT_ROOT 设置,请添加到 settings.py
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
在这种情况下,您将需要更改文件系统权限
以下代码对我有用
img_path = os.path.join(settings.STATIC_ROOT,"directory/image.jpg")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.