Table of contents
1. The principle of negative film effect
2. The effect principle of watermark
4. The complete code of negative film and watermark effect
1. The principle of negative film effect
Set the three-color value of the pixel to (255-original value. Set the image matrix as img, the code is as follows:
# 生成负片
b, g, r = cv2.split(img)
b = 255-b
g = 255-g
r = 255-r
2. The effect principle of watermark
Call the putText function, take the image matrix as the first parameter, and the output content as the second parameter, and directly output the watermark text on the image. code show as below:
加上水印
cv2.putText(img,'HELLO WORLD', (20,20), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2)
# 文本的位置在坐标(20,20),使用字体cv2.FONT_HERSHEY_PLAIN,字体大小为2.0,颜色为黑色(0,0,0),线条粗细为2。
cv2.putText(img,'HELLO MACHINE LEARNING', (20,100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2)
3. Example picture
4. The complete code of negative film and watermark effect
import cv2
import numpy as np
fn = r"C:\Users\LIHAO\Pictures\Saved Pictures\wallhaven-1pdk1v.jpg"
if __name__ == '__main__':
img = cv2.imread(fn)
w=img.shape[1]
h=img.shape[0]
# 生成负片
b, g, r = cv2.split(img)
b = 255-b
g = 255-g
r = 255-r
# 直接通过索引改变色彩分量
img[:,:,0]=b
img[:,:,1]=g
img[:,:,2]=r
# 加上水印
cv2.putText(img,'HELLO WORLD', (100,1000), cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2)
# 文本的位置在坐标(100,1000),使用字体cv2.FONT_HERSHEY_PLAIN,字体大小为2.0,颜色为黑色(0,0,0),线条粗细为2。
cv2.putText(img,'HELLO MACHINE LEARNING', (600,100), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2)
cv2.namedWindow('img')
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()