import cv2
# opencv讀取圖像
img = cv2.imread('./1.jpg', 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("ok.jpg",dst)
cv2.waitKey(0)
輸入:
輸出: