将图像的黑白颜色反转并保存
import cv2
# opencv读取图像
img = cv2.imread(r'E:\Python-workspace\OpenCV\OpenCV/YY.png', 1)
cv2.imshow('img', img)
img_shape = img.shape # 图像大小(565, 650, 3)
print(img_shape)
h = img_shape[0]
w = img_shape[1]
# 彩色图像转换为灰度图像(3通道变为1通道)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(gray.shape)
# 最大图像灰度值减去原图像,即可得到反转的图像
dst = 255 - gray
cv2.imshow('dst', dst)
cv2.imwrite("E:\Python-workspace\OpenCV\OpenCV/YY1.png",dst)#保存照片
cv2.waitKey(0)
原图:
黑白颜色反转后的图片: