解决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)
相关推荐
xsd202411187 小时前
步态识别算法全解析:从剪影提取到跨视角身份识别的技术拆解
人工智能
朝朝辞暮i8 小时前
VLA 系统学习第 3 课:从一次机器人示范,到真正送进神经网络的 Batch
人工智能·python·深度学习·神经网络·vla
Joy T8 小时前
Spring AI 接入已有 Java 项目的三种架构设计
java·人工智能·springai·ai入门·chatclient·ai service·ai能力接入
m4Rk_8 小时前
【论文阅读】Agent 记忆机制(94):MemGen——在推理过程中动态生成并织入潜在记忆
论文阅读·人工智能·学习·开源·github
IT_陈寒8 小时前
Redis卡顿的锅,这次真不是大key的错
前端·人工智能·后端
小鹿的周先生8 小时前
第11章-Structured-Output
开发语言·人工智能·python
175******631738 小时前
灰片调色适合什么素材
人工智能
大模型真好玩8 小时前
从 SDK 到成熟智能体:拆解 LangChain、Pi、DeepSeek Harness、Claude Code的本质区别
人工智能·agent·deepseek
程序哥聊面试8 小时前
什么是 Sandbox?给 AI Agent 划一道安全边界
人工智能·安全
大虾别跑9 小时前
ai-daily-2026-10-08
人工智能·chatgpt