import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
bk_img = cv2.imread(“0.jpg”)
#设置需要显示的字体
fontpath = “font/simsun.ttc”
font = ImageFont.truetype(fontpath, 32) # 32为字体大小
img_pil = Image.fromarray(bk_img)
draw = ImageDraw.Draw(img_pil)
#绘制文字信息# (100,300/350)为字体的位置,(255,255,255)为白色,(0,0,0)为黑色
draw.text((100, 300), “Hello World”, font = font, fill = (255, 255, 255))
draw.text((100, 350), “你好,世界”, font = font, fill = (255, 255, 255))
bk_img = np.array(img_pil)
cv2.imshow(“add_text”,bk_img) # 保存图片路径
cv2.waitKey()
cv2.imwrite(“add_text.jpg”,bk_img)
添加英文文字可直接使用plt或者cv2
如使用cv2.putText()函数添加