openCv-python无法以vs代码读取图像

问题描述 投票:-2回答:2

不过只是编码世界的新伙伴,但是今天,我碰巧遇到了一个烦人的问题,那就是在两天前完美运行的vs代码中运行openCv python脚本。自创建以来,我从未修改过该代码,它可以在Spider中运行,没有任何问题。

以下是弹出的错误消息:

(base) E:\UDEMY>C:/Users/ProBook/Anaconda3/python.exe e:/UDEMY/openCV/cvimg.py
<class 'NoneType'>
None
Traceback (most recent call last):
  File "e:/UDEMY/openCV/cvimg.py", line 7, in <module>
    print(img.shape) # print the dimanstion of the image interms of pixel
AttributeError: 'NoneType' object has no attribute 'shape'

(base) E:\UDEMY

由于程序正在通过Spider运行,所以我认为问题出在vs代码中

下面是python脚本:“”“

import cv2

img=cv2.imread("mycastle.jpg",1) #load the image

print(type(img)) #check the data type of image 
print(img) # print the matrix array of the image
print(img.shape) # print the dimanstion of the image interms of pixel
print(img.ndim) # checks the dimention of the image 2D or 3D etc

resized_img=cv2.resize(img,(800,768))
cv2.imshow("mycastle", resized_img)
cv2.imwrite("myNewCastle.jpg", resized_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

“”“

exact image i was using.

python image opencv
2个回答
0
投票

该代码在我的机器上可以正常工作,因此我将假定您的图像路径无效。尝试将下面的代码移至第4行。图像是否出现?

cv2.imshow("mycastle", img)

0
投票

您必须指定图像路径。转到图像,右键单击它,然后在属性中找到类似“ C:\ Users \ images \ 6.JPG”的路径。用双“ \”更改“ \”,然后重试。

img=cv2.imread("C:\\Users\\images\\mycastle.jpg",1)
© www.soinside.com 2019 - 2024. All rights reserved.