解决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)
相关推荐
nvvas10 小时前
Spring AI 2.0正式GA:Token砍掉64%,Java的AI反击战打响了
java·人工智能
PPIO派欧云10 小时前
DeepSeek V4.1-Flash 更新后,企业如何管理模型版本、成本与稳定性?
人工智能
ZPC821010 小时前
YOLO 识别串果西红柿果梗(果柄)完整方案(适配你的采摘机器人)
人工智能
lie..10 小时前
30天从零开始学AI应用开发(Day 1):机器学习、深度学习、大模型,到底是个啥关系
人工智能·python·大模型
Ivanqhz10 小时前
Unigram 算法
开发语言·人工智能·python·深度学习·mlir
小宋102111 小时前
Embedding 模型怎么无痛迁移:双写、回填、灰度切换与回滚实战
人工智能
冯一川11 小时前
Python 实现与 Ollama 运行的模型对话
人工智能·python
pt104311 小时前
Cisco Splunk for AI Operations:AIOps企业实践
运维·人工智能·自动化
龙腾AI白云11 小时前
大模型的幻觉怎么治
人工智能·机器学习·知识图谱
刘天远11 小时前
Agent系统接入编排:评分模型、状态机与Python门禁
数据库·人工智能·python