解决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)
相关推荐
新知图书6 分钟前
第 6 章DeepSeek的Function Calling与MCP应用实战
人工智能·智能体
xn713319 分钟前
Funes Agent Memory 实测:Codex 长期记忆召回、旧记忆污染与 no-answer 边界
人工智能·llm·ai编程
天空属于哈夫克325 分钟前
企业微信AI开发:如何让AI根据用户消息自动生成回复?
人工智能·microsoft
Raas10028 分钟前
AI网关和LiteLLM区别在哪?MAI Gateway(魔芋企业级AI网关)统一治理方案深度解析
大数据·人工智能·gateway·mai gateway·企业级产品
GlobalInfo33 分钟前
2026新能源汽车线束研究报告:市场规模与十五五风向
大数据·人工智能·新能源
William一直在路上1 小时前
GPT-6 Astra 研究报告——模型能力、API 演进与 Agent/MCP 生态影响分析
人工智能·gpt·llm·openai·astra
tntxia1 小时前
AI 应用开发生产落地实践指南
人工智能
林墨聊AIGC1 小时前
AI视频怎么做跳舞的动作效果:从入门到精通的舞蹈动画制作指南
大数据·人工智能·自动化·aigc·音视频
IT_陈寒1 小时前
Vite静态资源路径这个大坑害我调了一下午
前端·人工智能·后端
武哥聊编程1 小时前
【AI实战项目】基于SpringAI+Springboot+Vue的AI面试刷题训练平台
vue.js·人工智能·spring boot·springai