小褚 发表于 2024-4-13 13:10:49

python 解决OpenCV显示中文字符的方法汇总

因工作需要,要在图片中显示中文字符,并且要求速度足够快,在网上搜罗一番后,总结下几个解决方法。

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-pyft.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, pos+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   # div 64
      self.pen.y = y_pos << 6
      image = copy.deepcopy(img)
      for cur_char in text:
            self._face.load_char(cur_char)
            # kerning = self._face.get_kerning(prev_char, cur_char)
            # pen.x += kerning.x
            slot = self._face.glyph
            bitmap = slot.bitmap
            self.pen.x += 0
            self.cur_pen.x = self.pen.x
            self.cur_pen.y = self.pen.y - slot.bitmap_top * 64
            self.draw_ft_bitmap(image, bitmap, self.cur_pen, color)
            self.pen.x += slot.advance.x
            prev_char = cur_char
      return image
    def draw_ft_bitmap(self, img, bitmap, pen, color):
      '''
      draw each char
      :param bitmap: bitmap
      :param pen:    pen
      :param color:pen color e.g.(0,0,255) - red
      :return:       image
      '''
      x_pos = pen.x >> 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 != 0:
                  img = color
                  img = color
                  img = color
if __name__ == '__main__':
    # just for test
    import cv2
    line = '你好'
    img = np.zeros()
    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显示中文字符的文章就介绍到这了,更多相关python 显示中文字符内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/python/319225xkh.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: python 解决OpenCV显示中文字符的方法汇总