快捷搜索: 王者荣耀 脱发

python cv2 putText 显示中文的方法。

当 cv2.putText中有中文存在时,显示在屏幕上就是????,为了解决这个问题,并且在各个平台如win10,linux同时运行,代码如下:
    import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, np.ndarray)): # 判断是否OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 创建一个可以在给定图像上绘图的对象 draw = ImageDraw.Draw(img) # 字体的格式 fontStyle = ImageFont.truetype( "simsun.ttc", textSize, encoding="utf-8") # 绘制文本 draw.text((left, top), text, textColor, font=fontStyle) # 转换回OpenCV格式 return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) img = cv2ImgAddText(cv2.imread(img1.jpg), "hello中文", 10, 65, (0, 0 , 139), 20) cv2.imshow(show, img) cv2.waitkey(0)

然后把simsun.tcc文件拷贝到与.py文件同一路径下,放在linux下也可正常运行

经验分享 程序员 微信小程序 职场和发展