读图片+操作
#调入包
import cv2
#读入图片,'bear.jpg'是我事先准备好的图片(此处也可以填写文件路径)
#'0'读灰度图,'1'读颜色图,'-1'读阿尔法通道图
img = cv2.imread('bear.jpg',0)
#改变图片大小 (600,500)是尺寸
img = cv2.resize(img,(600,500)))
#显示图片,img后参数指的是显示哪张图片
cv2.imshow('bear',img)
#显示等待时间,'0'指的是任意键后停止
cv2.waitKey(0)
#关闭窗口
cv2.destroyAllWindows()
原图:
读灰度图:
存储图片
import cv2
#调入系统包
import os
p = cv2.imread('bear.jpg', -1 )
cv2.imshow('bear',p)
cv2.waitKey(0)
#在这里对要保存的文件名做同名检测
if os.path.isfile('bear1.jpg'):
cv2.destroyAllWindows()
print('存在图片储存失败')
else:
#如果没有同名文件则输出图片用imwrite来输出
cv2.imwrite('bear1.jpg',p)
cv2.destroyAllWindows()
print('成功')
效果图:
除此之外,还可以对视频进行操作,具体操作将在接下来笔记中进行讲解