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

 更新时间:2024年04月03日 11:19:17   作者:何时摆脱命运的束缚  
因工作需要,要在图片中显示中文字符,并且要求速度足够快,在网上搜罗一番后,总结下几个解决方法,对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-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   # 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[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显示中文字符的文章就介绍到这了,更多相关python 显示中文字符内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 超详细注释之OpenCV构建透明的叠加层

    超详细注释之OpenCV构建透明的叠加层

    这篇文章主要介绍了OpenCV构建透明的叠加层,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Python Collatz序列实现过程解析

    Python Collatz序列实现过程解析

    这篇文章主要介绍了Python Collatz序列实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • 详解python中的异常捕获

    详解python中的异常捕获

    这篇文章主要介绍了python中的异常捕获的相关资料,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • 解决PyCharm IDE环境下,执行unittest不生成测试报告的问题

    解决PyCharm IDE环境下,执行unittest不生成测试报告的问题

    这篇文章主要介绍了解决PyCharm IDE环境下,执行unittest不生成测试报告的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 在VS Code上搭建Python开发环境的方法

    在VS Code上搭建Python开发环境的方法

    这篇文章主要介绍了在VS Code上搭建Python开发环境的方法,需要的朋友可以参考下
    2018-04-04
  • Python Asyncio库之asyncio.task常用函数详解

    Python Asyncio库之asyncio.task常用函数详解

    Asyncio在经过一段时间的发展以及获取Curio等第三方库的经验来提供更多的功能,目前高级功能也基本完善。本文主要介绍了Asyncio库中asyncio.task常用函数的使用,需要的可以参考一下
    2023-03-03
  • Windows下python3.6.4安装教程

    Windows下python3.6.4安装教程

    这篇文章主要为大家详细介绍了Windows下python3.6.4安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • 基于python代码实现简易滤除数字的方法

    基于python代码实现简易滤除数字的方法

    今天小编就为大家分享一篇基于python代码实现简易滤除数字的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python格式化字符串的实战教程(使用占位符、format方法)

    python格式化字符串的实战教程(使用占位符、format方法)

    我们经常会用到%-formatting和str.format()来格式化,下面这篇文章主要给大家介绍了关于python格式化字符串的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-08-08
  • 深入了解Python Opencv数据增强

    深入了解Python Opencv数据增强

    常见的数据增强操作有:按比例放大或缩小图片、旋转、平移、水平翻转、改变图像通道等。本文将通过Python OpenCV实现这些操作,需要的可以参考一下
    2022-02-02

最新评论