1、绘制文本
putText(img: ndarray,#图片
text: str,#文字
org: Sequence[int], #文本字符串的左下角
fontFace: int,#字体
fontScale: float #字体大小,
color: Sequence[float],
thickness: int = ...,#宽度
lineType: int = ...,#类型默认为8
bottomLeftOrigin: bool = ...) #坐标起点位置,为true时起点在左下角,为false时起点在左上角
cv2.putText(img,"hello ",(100,100),cv2.FONT_HERSHEY_PLAIN,8,(0,0,255))
2.绘制中文
注意:Opencv是没有办法直接绘制中文的,会出现乱码,可以使用Pillow包
安装: pip install pillow
#导入字体文件
font=ImageFont.truetype("C:\\Windows\\Fonts\\simfang.ttf",15)#该路径下,是电脑中存在的字体
#创建一个pillow的图片
img_pil = Image.fromarray(img)
img_draw =ImageDraw.Draw(img_pil)
#利用img_draw绘制中文
img_draw.text((100,200), "你好", fill=(255, 0, 0),font=font)
#重新变回ndarray类型
img=np.array(img_pil)
cv2.imshow("img",img)
cv2.waitKey(0)