cv2.imread始终返回NoneType python 3.6

时间:2018-11-16 09:53:48

标签: python python-3.x opencv

cv2.imread始终返回NoneType

我有以下代码:

path = cv2.imread('D:/XXX/image.jpg')

返回的数组为Nonetype

但是当我使用

path = cv2.imread('image.jpg')

我得到正确的数组。 仅当我使用完整路径时,才会出现此问题。 我也尝试过反斜杠和双斜杠,但这没什么区别。之前有很多类似的问题,我还没有任何合适的答案。 文件路径和文件名均正确。

我正在使用Python版本3.6和OpenCV版本3.4.3

更新 我尝试使用tkinter filedialog.askopenfilename获取文件路径。仍然没有成功。

1 个答案:

答案 0 :(得分:-2)

您正在正确使用它,但是缺少参数。 呼叫cv2.imread(jpeg_file,0) yeild a ndtype array object

但是,请尝试此操作并确保图像文件的大小不为零。

img = cv2.imread(filename_name,0)

如文档所述

    Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.

Second argument is a flag which specifies the way image should be read.

cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
See the code below:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)

Cv2.imread usage