解决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)
相关推荐
格桑阿sir12 小时前
15-大模型智能体开发工程师:深度学习MCP协议(Model Context Protocol)
人工智能·ai·大模型·agent·sse·mcp·streamable http
程序员佳佳12 小时前
深度解析:向量引擎如何影响AI内容收录?附3个月实测数据
人工智能·gpt·自动化·ai写作·codex
feng145613 小时前
OpenSREClaw - AI 本体论思维
运维·人工智能
zhangxingchao13 小时前
AI应用开发八:RAG相关技术总结
前端·人工智能·后端
码农小旋风13 小时前
国内使用 Claude 的 5 种路径:网页、订阅、API 和企业方案怎么选
人工智能·chatgpt
清水寺小和尚13 小时前
MCP 协议拆解:从 JSON-RPC 信封到 Agent 全链路
人工智能
机器之心13 小时前
当Token飙到天文数字,高通用「计算连续体」重搭智能体新基建
人工智能·openai
weixin_4684668513 小时前
液态神经网络新手入门与实战指南
人工智能·深度学习·神经网络·ai·机器视觉·液态神经网络
机器之心13 小时前
一夜之间,ChatGPT与Codex合并了
人工智能·openai
机器之心13 小时前
老黄的Cosmos 3刚发一天,就被一家中国公司反超了
人工智能·openai