python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。
因工作需要,要在图片中显示中文字符,并且要求速度足够快,在网上搜罗一番后,总结下几个解决方法。
1.方法一:转PIL后使用PIL相关函数添加中文字符
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
# cv2读取图片,名称不能有汉字
img = cv2.imread('pic1.jpeg')
# cv2和PIL中颜色的hex码的储存顺序不同
cv2img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pilimg = Image.fromarray(cv2img)
# PIL图片上打印汉字
draw = ImageDraw.Draw(pilimg) # 图片上打印
#simsun 宋体
font = ImageFont.truetype("simsun.ttf", 40, encoding="utf-8")
#位置,文字,颜色==红色,字体引入
draw.text((20, 20), "你好", (255, 0, 0), font=font)
# PIL图片转cv2 图片
cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
cv2.imshow("img", cv2charimg)
cv2.waitKey (0)
cv2.destroyAllWindows()
存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms
2.方法二:opencv重新编译带freetype
编译过程略。
import cv2
import numpy as np
# 创建一个黑色的图像
img = np.zeros((300, 500, 3), dtype=np.uint8)
# 中文文本
text = '你好,OpenCV!'
# 设置字体相关参数
font_path = 'path/to/your/chinese/font.ttf' # 替换为你的中文字体文件路径
font_size = 24
font_color = (255, 255, 255)
thickness = 2
# 使用 truetype 字体加载中文字体
font = cv2.freetype.createFreeType2()
font.loadFontData(font_path)
# 在图像上放置中文文本
position = (50, 150)
font.putText(img, text, position, font_size, font_color, thickness=thickness)
# 显示图像
cv2.imshow('Image with Chinese Text', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
缺点:编译过程复杂繁琐,容易报一些列的错误,编译时确保已安装第三方库freetype和harfbuzz,并且配置编译参数时需打开freetype。
3.方法三:使用freetype-py
pip install freetype-py
ft.py
import numpy as np
import freetype
import copy
import pdb
import time
class PutChineseText(object):
def __init__(self, ttf, text_size):
self._face = freetype.Face(ttf)
hscale = 1.0
self.matrix = freetype.Matrix(int(hscale)*0x10000, int(0.2*0x10000),int(0.0*0x10000), int(1.1*0x10000))
self.cur_pen = freetype.Vector()
self.pen_translate = freetype.Vector()
self._face.set_transform(self.matrix, self.pen_translate)
self._face.set_char_size(text_size * 64)
metrics = self._face.size
ascender = metrics.ascender/64.0
#descender = metrics.descender/64.0
#height = metrics.height/64.0
#linegap = height - ascender + descender
self.ypos = int(ascender)
self.pen = freetype.Vector()
def draw_text(self, image, pos, text, text_color):
'''
draw chinese(or not) text with ttf
:param image: image(numpy.ndarray) to draw text
:param pos: where to draw text
:param text: the context, for chinese should be unicode type
:param text_size: text size
:param text_color:text color
:return: image
'''
# if not isinstance(text, unicode):
# text = text.decode('utf-8')
img = self.draw_string(image, pos[0], pos[1]+self.ypos, text, text_color)
return img
def draw_string(self, img, x_pos, y_pos, text, color):
'''
draw string
:param x_pos: text x-postion on img
:param y_pos: text y-postion on img
:param text: text (unicode)
:param color: text color
:return: image
'''
prev_char = 0
self.pen.x = x_pos 6
y_pos = pen.y >> 6
cols = bitmap.width
rows = bitmap.rows
glyph_pixels = bitmap.buffer
for row in range(rows):
for col in range(cols):
if glyph_pixels[row*cols + col] != 0:
img[y_pos + row][x_pos + col][0] = color[0]
img[y_pos + row][x_pos + col][1] = color[1]
img[y_pos + row][x_pos + col][2] = color[2]
if __name__ == '__main__':
# just for test
import cv2
line = '你好'
img = np.zeros([300,300,3])
color_ = (0,255,0) # Green
pos = (40, 40)
text_size = 24
ft = PutChineseText('font/simsun.ttc',text_size=20)
t1 = time.time()
image = ft.draw_text(img, pos, line, color_)
print(f'draw load . ({time.time() - t1:.3f}s)')
cv2.imshow('ss', image)
cv2.waitKey(0)
缺点:每个字符耗时在0.3~1ms左右,耗时略大。
4.方法四:使用OpenCV5.0
4.1 编译opencv5.x版本
编译过程较复杂,不推荐。
4.2 使用rolling版本
卸载原先安装的opencv
pip uninstall opencv-python
pip uninstall opencv-contrib-python
安装rolling版本
pip install opencv-python-rolling
pip install opencv-contrib-python-rolling
安装完毕后,cv2.putText即可支持中文字符。
缺点:5.0版本暂未正式发布,可能存在不稳定情况
优点:耗时几乎没有
python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。python 解决OpenCV显示中文字符 存在的缺点:cv2->pil 中 Image.fromarray(img)存在耗时4~5ms。