[text]绘制中文字符
使用putText时发现中文字符会出现乱码。在网上找了很多资料
- How to draw Chinese text on the image using
cv2.putText
correctly? (Python+OpenCV) - Changing font family in OpenCV Python using PIL
- Error 'module' object has no attribute 'freetype'
- freetype的python实现测试
- python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码
- AttributeError: module 'cv2.cv2' has no attribute 'freetype' in OpenCV
- How to draw arabic text on the image using
cv2.putText
correctly? (Python+OpenCV) - How to draw Chinese text on the image using
cv2.putText
correctly? (Python+OpenCV) - OpenCV module for drawing UTF-8 strings with freetype2
总结下来共4
种实现方式:
- 先转成
PIL
格式,再进行中文字符绘制,最后再转回Numpy
数组 - 直接使用
Freetype
库进行图像绘制 - 自编译
skvark/opencv-python
,加入FREETYPE
模块 - 自编译
OpenCV
源码,加入FREETYPE
模块
上面四种实现方式的绘制速度依次是1 < 2 < 3 < 4
,其中,后两种的实现参考
使用freetype
进行中文字符绘制:
import cv2
import time
import numpy as np
img = np.zeros((300, 512, 3), dtype=np.uint8)
ft = cv2.freetype.createFreeType2()
ft.loadFontData(fontFileName='/home/zj/work/expressbill/PaddleOCR/doc/simfang.ttf', id=0)
start = time.time()
ft.putText(img=img,
text='Quicfadf发达发afd3142 Fox',
org=(15, 70),
fontHeight=30,
color=(255, 255, 255),
thickness=-1,
line_type=cv2.LINE_AA,
bottomLeftOrigin=True)
print('freetype need: {}'.format(time.time() - start))
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.imwrite('image.png', img)
################## 输出
freetype need: 0.00032019615173339844