解决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)
相关推荐
啵啵啵鱼4 小时前
Chrome 内存泄漏诊断指南
人工智能
Mirawork5 小时前
AI 低代码建站原理:从自然语言需求到在线应用,拆解 AI 生成网页的完整技术链路
人工智能·低代码·ai·ai编程
xsd202411185 小时前
rea-video开源 AI 视频逆向工具
人工智能
thinking_talk6 小时前
AI 自动诊断慢 SQL 怎么落地:五层能力怎么分,以及生产可用要过哪几道门槛
数据库·人工智能·sql·慢sql
码上观世界6 小时前
IndexTTS接入实时交互数字人系统实践
人工智能·实时互动
具身AGI6 小时前
进博会机器人,隔桌对打乒乓球
人工智能·机器人
逐米时代6 小时前
「制造新解法」行业对标方法:借鉴而不照搬的最佳实践
人工智能·算法·制造
小追兵6 小时前
Claude Managed Agents 自托管是什么?企业如何控制 Agent Runtime、Session 与数据?
人工智能
Wellijohn7 小时前
上下文工程实战:把大模型 Agent 从「能跑」做到「上线不掉链子」
人工智能·llm·agent
JavaGuide7 小时前
Gemini 杀疯了,反重力给我用爽了!分享 8 个 Antigravity 使用技巧
人工智能·后端