我试图读取图像和调整我的主目录文件中的图像,但没有工作请帮助如何读取图像和调整大小。在
import cv2
from PIL import Image
img = cv2.resize(cv2.read('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-103-dab0f11a9e2d> in <module>()
1 import cv2
2 from PIL import Image
----> 3 img = cv2.resize(cv2.read('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))
AttributeError: module 'cv2.cv2' has no attribute 'read'
使用glob的模块:
这里的错误是您给cv2.imread一个目录,但它只接受图像路径作为输入,否则会抛出错误。在
所以我们可以做的就是使用操作系统模块解析文件夹中的所有文件,然后逐个读取图像,然后调整大小并对其执行其他操作。在
另外,我认为你在这个文件夹里只有图像。如果里面有其他类型的文件或其他文件夹,可以在os.path.join操作系统行。在
希望这有帮助。在
要读取特定扩展名的所有图像,例如“*.png”,可以使用
cv::glob
函数然后调整缓冲区中每个图像的大小
^{pr2}$重写为python应该很容易,因为几乎所有函数/数据类型在python中都有等价的。在
代码被分为多个部分,而不是一个行程序,因为它更具可读性,至少对我来说是这样。在
相关问题 更多 >
编程相关推荐