opencv 图片不能显示中文,不支持中文

时间:2025-04-26 07:29:49

由于opencv中的不支持中文显示,这里使用Pillow来解决这个问题,我写了一个简单的demo。

1.cv2putText_chinese.py  这个文件主要定义了转换函数

import cv2
import numpy
from PIL import Image,ImageDraw,ImageFont

def cv2ImgAddText(img,text,left,top,textColor,textSize):
    if(isinstance(img,)):
        img=((img,cv2.COLOR_BGR2RGB))
    draw =(img)
    ttf="/usr/share/fonts/opentype/noto/"     #linux中的中文字体格式一般在/usr/share/fonts/opentype/noto下
    fontStyle=(
    ttf,textSize,encoding="utf-8")
    ((left,top),text,textColor,font=fontStyle)
    return ((img),cv2.COLOR_RGB2BGR)

import cv2

from cv2putText_chinese import cv2ImgAddText

if __name__ == '__main__':

    bmp=('./')

    img=cv2ImgAddText(bmp,' 哈哈哈哈',10,15,(255,255,255),10)

    ('show',img)

    (2000)