直接通过cv2.putText()写入的文字只能是英文,虽然opencv以字符串形式接收,但对于中文,会显示问号”???“。
找到一个可行的办法,可以应急
封装代码:
#coding=utf-8#中文乱码处理import cv2import numpyfrom PIL import Image, ImageDraw, ImageFontdef cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):if (isinstance(img, numpy.ndarray)): #判断是否OpenCV图片类型img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))draw = ImageDraw.Draw(img)fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="utf-8")draw.text((left, top), text, textColor, font=fontText)return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)--------------------- 作者:谱写 来源:CSDN 原文:/baidu_37366055/article/details/81627185 版权声明:本文为博主原创文章,转载请附上博文链接!
代码调用:
img = cv2ImgAddText(img, "大家好,我是星爷", 140, 60, (255, 255, 0), 20)
原博文有详细说说明,感谢博主。转载自:/baidu_37366055/article/details/81627185
如果觉得《【图像处理基础知识】python+opencv显示中文》对你有帮助,请点赞、收藏,并留下你的观点哦!