import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
def create_img():
# 打开图片
bk_img = cv2.imread("invite.png")
# 设置需要显示的字体
fontpath = "cimg//font//msyh.ttc"
font = ImageFont.truetype(fontpath, 32)
img_pil = Image.fromarray(bk_img)
draw = ImageDraw.Draw(img_pil)
# 绘制文字信息
draw.text((100, 300), "Hello World", font = font, fill = (255, 255, 255))
draw.text((100, 350), "邀请信息:测试邀请活动13222222222", font = font, fill = (255, 255, 255))
bk_img = np.array(img_pil)
cv2.imshow("add_text",bk_img)
cv2.waitKey()
# 输出图片的地址
cv2.imwrite("cimg//cout//add_text.jpg",bk_img)
# 掉用函数
create_img()
运行结果: