解决opencv在图片中添加中文字体乱码问题
需要事先安装PIL库
python
from PIL import Image, ImageDraw, ImageFont
import cv2
def cv2_img_add_text(img, text_str, left, top, text_color, text_size):
if isinstance(img, np.ndarray):
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype("msjh.ttc", text_size, encoding="utf-8")
draw.text((left, top), text_str, text_color, font=fontText)
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
# frame为opencv读取的数据图像,BGR通道格式
position_x=20
position_y=20
frame = cv2_img_add_text(frame, str(COUNTER_EYE),position_x, position_y, (55, 255, 155), 20)