python 解决OpenCV显示中文字符

2024-03-31 1552阅读

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

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
        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版本暂未正式发布,可能存在不稳定情况

优点:耗时几乎没有

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]