#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
A font library is needed here, I chose Microsoft Yahei, and the address is in
https://download.csdn.net/download/lianbus/12233490