解决opencv在图片中添加中文字体乱码问题

解决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)
相关推荐
泉城老铁8 小时前
60天GitHub星标超Linux、腾讯总部排队"领虾"——AI从"会聊天"到"会做事",一场正在发生的范式革命
人工智能
嘻嘻的AI日记8 小时前
AI 知识库更合规:政企数据安全检索与智能应用合规体系
大数据·人工智能
空中湖9 小时前
Spring AI Function Calling 完全指南:让 AI 查数据库、调 API、发通知
数据库·人工智能·spring
沫儿笙9 小时前
弧焊机器人氩气节气装置
人工智能·机器人
未来和明天9 小时前
领嵌AI边缘计算盒子,筑牢厂区安全防线
人工智能
中微极客9 小时前
AI Agent与TinyML边缘部署:OAuth 2.0集成实战
人工智能
是上好佳佳佳呀9 小时前
【机器学习|DAY01】机器学习概述
人工智能·机器学习
沉默王二9 小时前
又一个顶级 AI 终端诞生了!Kaku 开源,实测安装配置+分屏对决
人工智能·openai·claude
xcLeigh9 小时前
Doubao-Seed-Evolving大模型接入教程|搭建全品类提示词+AI工具导航网页
前端·人工智能·python·ai·html·ai开发·豆包
不如语冰9 小时前
AI大模型入门-模块导入import
数据结构·人工智能·pytorch·python