解决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)
相关推荐
水上冰石4 小时前
【MHS协议】第四章:ESP32 接入 MHS 协议实战:从零构建一个 MHS 兼容设备
人工智能·架构·机器人
知了一笑5 小时前
个体看衰AI,企业加速转型
人工智能·ai
飞哥数智坊6 小时前
一只虾到多只虾:先聊聊我为什么要“拆虾”
人工智能
飞哥数智坊6 小时前
架构图 SKILL 封装好了,顺便做了虾的适配
人工智能
冬奇Lab6 小时前
Code Agent 解剖(16):AgentTeams——为什么一个 agent 不够用?
人工智能
东风破_6 小时前
聊天记录越来越长怎么办?从消息数量截断到 Token 截断
人工智能
冬奇Lab6 小时前
开源项目第204期:LoopX — 长周期 Agent 控制平面,跑在 Codex/Claude Code 之上的状态管理层
人工智能·开源
ZGIAI7 小时前
旧模型下线前,客服 Agent 怎么迁移
人工智能·架构
东风破_7 小时前
程序重启后,AI 为什么把你忘了?从 InMemory 到持久化 Memory
人工智能
ZGIAI7 小时前
客服知识库更新后,怎么批量验收
人工智能·架构