import cv2
import numpy as np
# 读取图片
img = cv2.imread('lena.jpg')
cv2.imshow('lena', img)
cv2.waitKey(0) # 持续函数
print(img)
height = img.shape[0]
width = img.shape[1]
negative_file = np.zeros((height, width, 3))
# BGR图像拆分为3通道
b, g, r = cv2.split(img)
r = 255-r
b = 255-b
g = 255-g
negative_file[:, :, 0] = b
negative_file[:, :, 1] = g
negative_file[:, :, 2] = r
print(negative_file)
cv2.imwrite("negative_file.jpg", negative_file)
效果展示: