python opencv输出中文

opencv在视频中通过putText函数能添加文字,但对于中文则无能为力。一般需要FreeType字体库进行处理,在python可以通过PIL转换一下。
现将在视频中添加中文封装成函数如下:

def paint_chinese_opencv(im,chinese,pos,color):
    img_PIL = Image.fromarray(cv2.cvtColor(im,cv2.COLOR_BGR2RGB))
    font = ImageFont.truetype('NotoSansCJK-Bold.ttc',25)
    fillColor = color #(255,0,0)
    position = pos #(100,100)
    if not isinstance(chinese,unicode):
        chinese = chinese.decode('utf-8')
    draw = ImageDraw.Draw(img_PIL)
    draw.text(position,chinese,font=font,fill=fillColor)

    img = cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)
    return img
使用方法:
paint_chinese_opencv(image,str,point,color)
//Mat 图片,string "中文",Point (30,30),color (0,0,255)

ubuntu字体通常保存在:/usr/share/fonts/opentype/noto/
可通过locate *.ttc查找

猜你喜欢

转载自blog.csdn.net/san_junipero/article/details/79914773
今日推荐