解决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)
相关推荐
林伽一10 小时前
林伽一 · AI科技日报 | 2026年08月15日
人工智能
2501_9423895510 小时前
时钟组件支持自由拖拽缩放
人工智能·散列表·启发式算法·宽度优先·图搜索算法
小马过河R10 小时前
不只是又一个 Agent 框架:DeepSeek Harness 如何重新定义“可组合”
人工智能·机器学习·系统架构·agent·ai编程·harness
蓝速科技11 小时前
蓝速科技 3D 全息舱 AI 数字人一体机全尺寸选型实测指南
人工智能·科技·3d
AndrewHZ11 小时前
图像处理入门006:图像质量评价三剑客:PSNR / SSIM / MSE 原理与代码实战
图像处理·计算机视觉·cv·psnr·rse·ssim
lifallen11 小时前
DeepSeek Harness:把 Agent 做成可替换的运行时插件树
人工智能·学习·ai·开源软件·ai编程
刀锋000111 小时前
从0到1手搓生产级 AI Agent:LangGraph 1.2 + LangChain 1.3 保姆级实战(全部代码已跑通)
人工智能·python·langchain·ai agent·langgraph
水如烟11 小时前
孤能子视角:因果论——方向、锁定与必然感:关系场中归因链的生成语法
人工智能
老兵发新帖11 小时前
OSD和视频流接口随机出现net::ERR_CONNECTION_RESET问题分析总结
人工智能
海兰11 小时前
【开源工具】BlueKing Lite —— AI 原生的轻量运维平台(二)
运维·人工智能·开源