python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码

opencv图片写入中文(汉字)有两方法:

方法一:

python+opencv+freetype

https://blog.csdn.net/wyx100/article/details/75579581

方法二:

python+opencv+PIL

https://blog.csdn.net/wyx100/article/details/80412101


效果展示


开发环境配置

opencv+opencv_contrib 人脸识别和检测 python开发环境快速搭建(30分钟)图文教程

http://blog.csdn.net/wyx100/article/details/73008528

python+freetype配置

http://blog.csdn.net/wyx100/article/details/73527117

项目完整文档下载(源代码+字体+图片)

http://download.csdn.net/download/wyx100/9905823

报错 AttributeError: module 'cv2.face' has no attribute 'createEigenFaceRecognizer'

原因:版本问题,未安装

opencv_contrib,所以

model = cv2.face.createEigenFaceRecognizer() 行找不到face
下载地址

解决:更换版本 

详细见 http://blog.csdn.net/wyx100/article/details/73008324

完整字体下载

http://download.csdn.net/detail/o8xv0123/4589166

所有常用中英文ttf字体包,包含几个手写字体

包括:times new roman,中山行书百年纪念版,calibri,Christopherhand,DejaVuSansMono,方正兰亭黑,James Fajardo,Monaco,微软雅黑,仿宋,黑体,楷体,宋体,yahei_mono,仿宋_GB2312,楷体_GB2312,迷你简行楷碑。

项目源代码

1.主文件

hzTest.py

    
    
  1. #-*- coding: utf-8 -*-
  2. import cv2
  3. import ft2
  4. img = cv2.imread( 'pic/lena.jpg')
  5. line = '你好,我是 lena'
  6. color = ( 0, 255, 0) # Green
  7. pos = ( 3, 3)
  8. text_size = 24
  9. # ft = put_chinese_text('wqy-zenhei.ttc')
  10. ft = ft2.put_chinese_text( 'msyh.ttf')
  11. image = ft.draw_text(img, pos, line, text_size, color)
  12. name = u'图片展示'
  13. cv2.imshow(name, image)
  14. cv2.waitKey( 0)

2.中文处理文件(类)

ft2.py 
该文件也可以单独执行测试,包含主函数

    
    
  1. # -*- coding: utf-8 -*-
  2. # http://blog.csdn.net/zizi7/article/details/70145150
  3. '''
  4. ##################################################
  5. # tools #
  6. #------------------------------------------------#
  7. # draw chinese text using freetype on python2.x # #
  8. # 2017.4.12 #
  9. ##################################################
  10. '''
  11. import numpy as np
  12. import freetype
  13. import copy
  14. import pdb
  15. class put_chinese_text(object):
  16. def __init__(self, ttf):
  17. self._face = freetype.Face(ttf)
  18. def draw_text(self, image, pos, text, text_size, text_color):
  19. '''
  20. draw chinese(or not) text with ttf
  21. :param image: image(numpy.ndarray) to draw text
  22. :param pos: where to draw text
  23. :param text: the context, for chinese should be unicode type
  24. :param text_size: text size
  25. :param text_color:text color
  26. :return: image
  27. '''
  28. self._face.set_char_size(text_size * 64)
  29. metrics = self._face.size
  30. ascender = metrics.ascender/ 64.0
  31. #descender = metrics.descender/64.0
  32. #height = metrics.height/64.0
  33. #linegap = height - ascender + descender
  34. ypos = int(ascender)
  35. if not isinstance(text, unicode):
  36. text = text.decode( 'utf-8')
  37. img = self.draw_string(image, pos[ 0], pos[ 1]+ypos, text, text_color)
  38. return img
  39. def draw_string(self, img, x_pos, y_pos, text, color):
  40. '''
  41. draw string
  42. :param x_pos: text x-postion on img
  43. :param y_pos: text y-postion on img
  44. :param text: text (unicode)
  45. :param color: text color
  46. :return: image
  47. '''
  48. prev_char = 0
  49. pen = freetype.Vector()
  50. pen.x = x_pos << 6 # div 64
  51. pen.y = y_pos << 6
  52. hscale = 1.0
  53. matrix = freetype.Matrix(int(hscale)* 0x10000L, int( 0.2* 0x10000L),\
  54. int( 0.0* 0x10000L), int( 1.1* 0x10000L))
  55. cur_pen = freetype.Vector()
  56. pen_translate = freetype.Vector()
  57. image = copy.deepcopy(img)
  58. for cur_char in text:
  59. self._face.set_transform(matrix, pen_translate)
  60. self._face.load_char(cur_char)
  61. kerning = self._face.get_kerning(prev_char, cur_char)
  62. pen.x += kerning.x
  63. slot = self._face.glyph
  64. bitmap = slot.bitmap
  65. cur_pen.x = pen.x
  66. cur_pen.y = pen.y - slot.bitmap_top * 64
  67. self.draw_ft_bitmap(image, bitmap, cur_pen, color)
  68. pen.x += slot.advance.x
  69. prev_char = cur_char
  70. return image
  71. def draw_ft_bitmap(self, img, bitmap, pen, color):
  72. '''
  73. draw each char
  74. :param bitmap: bitmap
  75. :param pen: pen
  76. :param color: pen color e.g.(0,0,255) - red
  77. :return: image
  78. '''
  79. x_pos = pen.x >> 6
  80. y_pos = pen.y >> 6
  81. cols = bitmap.width
  82. rows = bitmap.rows
  83. glyph_pixels = bitmap.buffer
  84. for row in range(rows):
  85. for col in range(cols):
  86. if glyph_pixels[row*cols + col] != 0:
  87. img[y_pos + row][x_pos + col][ 0] = color[ 0]
  88. img[y_pos + row][x_pos + col][ 1] = color[ 1]
  89. img[y_pos + row][x_pos + col][ 2] = color[ 2]
  90. if __name__ == '__main__':
  91. # just for test
  92. import cv2
  93. line = '你好'
  94. img = np.zeros([ 300, 300, 3])
  95. color_ = ( 0, 255, 0) # Green
  96. pos = ( 3, 3)
  97. text_size = 24
  98. #ft = put_chinese_text('wqy-zenhei.ttc')
  99. ft = put_chinese_text( 'msyh.ttf')
  100. image = ft.draw_text(img, pos, line, text_size, color_)
  101. cv2.imshow( 'ss', image)
  102. cv2.waitKey( 0)

遇到问题、分享经验欢迎加入QQ群:452205574


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值