解决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)
相关推荐
深小乐13 小时前
AI 周刊【2026.04.13-04.19】:中美差距减小、Claude Opus 4.7发布、国产算力突围
人工智能
深小乐13 小时前
从 AI Skills 学实战技能(七):让 AI 自动操作浏览器
人工智能
workflower13 小时前
人机交互部分OOD
运维·人工智能·自动化·集成测试·人机交互·软件需求
lanker就是懒蛋13 小时前
深度学习Q&A:手写反向传播与OOM排查的深层逻辑
人工智能·深度学习
Old Uncle Tom13 小时前
Claude Code 记忆系统分析2
人工智能·ai·agent
小和尚同志13 小时前
skill-creator 1 —— 快速创建你的专属 skill
人工智能·aigc
skywalk816313 小时前
近期有什么ai的新消息,新动态? 2026.4月
人工智能
庄小焱14 小时前
【AI模型】——RAG索引构建与优化
人工智能·ai·向量数据库·ai大模型·rag·rag索引·索引构建与优化
STLearner14 小时前
WSDM 2026 | 时间序列(Time Series)论文总结【预测,表示学习,因果】
大数据·论文阅读·人工智能·深度学习·学习·机器学习·数据挖掘
玩转单片机与嵌入式14 小时前
不会 Python、不会深度学习,也能在STM32上跑AI模型吗?
人工智能·单片机·嵌入式硬件·嵌入式ai