algN/vodsdk/test/水印/视频添加文字水印1.py

35 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
# PIL添加水印
def cv2AddChineseText(img, text, position, textColor=(0, 255, 0), textSize=30):
if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"../font/simsun.ttc", textSize, encoding="utf-8")
# 绘制文本
draw.text(position, text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
img=cv2.imread("C:\\Users\\chenyukun\\Pictures\\Camera Roll\\a.jpg") # 导入我们需要添加水印的图片
# RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# RGB_img = img
# numpy.zeros(shapedtype=floatorder = 'C')
# np.zeros返回给定形状和类型的新数组用0填充。
# shape: int 或 int 的元组
# dtype: 类型
# uint8是专门用于存储各种图像的包括RGB灰度图像等范围是从0255
# blank_img = np.zeros(shape=(RGB_img.shape[0],RGB_img.shape[1],3), dtype=np.uint8)
# font = cv2.FONT_HERSHEY_TRIPLEX
# 添加水印的文字内容
# fontScale字体大小
# cv2.putText(图片img,“文本内容”,(左下角坐标),字体,字体大小,(颜色),线条粗细,线条类型)
frame=cv2AddChineseText(img, "拓恒技术", (123, 123),(0, 255, 0), 30)
cv2.imshow("Watermarked Image", frame)
cv2.imwrite("watermarked.jpg", frame)
cv2.waitKey(100000)
cv2.destroyAllWindows()