Opencv显示中文

#coding=utf-8
 
import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont
 


class DisplayChinese:
    def __init__(self):
        self.textSize=20
        self.fontText = ImageFont.truetype("/home/guo/eng/test/msyh.ttf", self.textSize, encoding="utf-8")

    def DrawImage(self,img,text,pos,color):
        img = Image.fromarray(img)
        draw = ImageDraw.Draw(img)
        draw.text(pos, text, color, font=self.fontText)
        return numpy.asarray(img)
    
dc=DisplayChinese()
cap = cv2.VideoCapture(0)
ret=True
pos=(140, 60)
color=(0, 255, 0)

while ret:
    ret, frame1 = cap.read()    
    if ret==False:
        print("one result")
        continue    
    frame1 = dc.DrawImage(frame1,"大家好",pos,color)
    cv2.imshow("frame1",frame1)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

 

这里面需要一个字库,我选了微软雅黑,地址放在

https://download.csdn.net/download/lianbus/12233490

 

 

你可能感兴趣的:(编程问题)