解决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)
相关推荐
阿里云大数据AI技术2 小时前
阿里云Flink + Fluss年度发布:Streaming Agent 与湖流一体
人工智能·flink·agent
萨小耶2 小时前
AI 面试答题链路源码分析:Redis 幂等、题目锁与状态机如何配合
人工智能·redis·面试
Java后端的Ai之路3 小时前
【AIAgent项目-AI生成PPT-09】灵活排版
前端·vue.js·人工智能·python·powerpoint
Mininglamp_27183 小时前
Human-AI Co-Writing With Full Accept/Reject Control on Every Edit
人工智能
X54先生(人文科技)3 小时前
ELR-SELLM_硅基伙伴通过对话碰撞个性涌现的推理架构_v10
人工智能·深度学习·架构·零知识证明
CV爱数码3 小时前
论文精读|RED-CNN:残差编解码网络,低剂量 CT 去噪的经典基线
网络·人工智能·算法·cnn·ct·重建·x-ray
Sailing3 小时前
【封闭结束3】ReAct 循环:从一次 Function Call 到多步 Agent
人工智能·面试·agent
AIyy8663 小时前
AI任务在关机后继续运行的三种实现思路
人工智能
万粉变现经纪人3 小时前
如何解决 pip install 安装报错 检测到 yanked 版本需更换可用版本 问题
人工智能·python·scrapy·numpy·pandas·pyqt·pip
mudtools3 小时前
一份声明,多份产物:飞书 SDK 的 AI 工具面是怎么生成的
人工智能·.net·ai编程