putText 显示中文为 ???

# https://blog.csdn.net/continuing1234/article/details/120139922
# https://blog.csdn.net/weixin_45914748/article/details/138718783?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-5-138718783-blog-120139922.235%5Ev43%5Econtrol&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-5-138718783-blog-120139922.235%5Ev43%5Econtrol&utm_relevant_index=10
# https://blog.csdn.net/qq_35165004/article/details/132738991
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
# img--cv2打开的图像 lt--左上 rb--右下 text--文本 color-BGR格式 thick--框的宽度 text_size--文本大小
def draw_rect_and_chinese(img, lt, rb, text, color, thick=2, text_size=20):
    cv2.rectangle(img, lt, rb, color, thickness=thick, lineType=cv2.LINE_AA)
    img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    draw = ImageDraw.Draw(img)
    # 参考3中有simsun.ttc的下载地址,下载后把"font/simsun.ttc"替换为ttc文件的路径即可。
    fontStyle = ImageFont.truetype("font/simsun.ttc", text_size, encoding="utf-8")
    draw.text((lt[0], lt[1]), text, tuple((color[2],color[1],color[0])), font=fontStyle)
    img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
    return img

img = cv2.imread("2niu.png")
img = draw_rect_and_chinese(img, (8,29),(320,251), "牛niu1", (0,0,255))
img = draw_rect_and_chinese(img, (359,39),(605,247), "牛niu2", (255,0,0))
cv2.imwrite("result.png", img)

结果:

参考:

1、解决cv2.putText能显示中文为“????”的问题_ubuntu cv2.puttext中文显示问号-CSDN博客

2、解决cv2.putText()不能显示中文标签_cv2.puttext中文-CSDN博客

3、Liunx环境安装字体(simsun为例)_simsun.ttc-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值