解决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)
相关推荐
蝎子莱莱爱打怪35 分钟前
AI Agent 相关知识扫盲:16 个概念+11张图+38个开源项目推荐
人工智能·github·agent
甲维斯1 小时前
Fable+Codex 《坦克大战3D》双端发布了!
人工智能·ai编程·游戏开发
掘金一周2 小时前
企业中要做智能体,最佳的方案是什么? | 沸点周刊 6.18
前端·人工智能·ai编程
雪隐2 小时前
个人电脑玩AI-04让5060 Ti给你打工——本地claude code编程助理
人工智能·后端
洛宇2 小时前
再谈 AI 时代,程序员的失眠问题。
人工智能
百度Geek说3 小时前
harness-pilot 给代码库加一套"规则说明书"和"自动检查器"
人工智能
程序员cxuan3 小时前
分享一下我最近常用的 10 个 Codex 小技巧。
人工智能·后端·程序员
用户337922545683 小时前
基于 OKF + RAG 构建 Text2SQL 语义层:让 LLM 真正理解你的数据库
人工智能
把所有砖敲烂3 小时前
MiniMax M3 深度实测:单卡部署、代码生成与性能全解析
人工智能