python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码
2017-07-21 00:30
1066 查看
效果展示
开发环境配置
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
下载地址
https://github.com/opencv/opencv_contrib
解决:更换版本
详细见 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#-*- coding: utf-8 -*- import cv2 import ft2 img = cv2.imread('pic/lena.jpg') line = '你好,我是 lena' color = (0, 255, 0) # Green pos = (3, 3) text_size = 24 # ft = put_chinese_text('wqy-zenhei.ttc') ft = ft2.put_chinese_text('msyh.ttf') image = ft.draw_text(img, pos, line, text_size, color) name = u'图片展示' cv2.imshow(name, image) cv2.waitKey(0)
2.中文处理文件(类)
ft2.py该文件也可以单独执行测试,包含主函数
# -*- coding: utf-8 -*- # http://blog.csdn.net/zizi7/article/details/70145150 ''' ################################################## # tools # #------------------------------------------------# # draw chinese text using freetype on python2.x # # # 2017.4.12 # ################################################## ''' import numpy as np import freetype import copy import pdb class put_chinese_text(object): def __init__(self, ttf): self._face = freetype.Face(ttf) def draw_text(self, image, pos, text, text_size, 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 ''' 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 ypos = int(ascender) if not isinstance(text, unicode): text = text.decode('utf-8') img = self.draw_string(image, pos[0], pos[1]+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 pen = freetype.Vector() pen.x = x_pos << 6 # div 64 pen.y = y_pos << 6 hscale = 1.0 matrix = freetype.Matrix(int(hscale)*0x10000L, int(0.2*0x10000L),\ int(0.0*0x10000L), int(1.1*0x10000L)) cur_pen = freetype.Vector() pen_translate = freetype.Vector() image = copy.deepcopy(img) for cur_char in text: self._face.set_transform(matrix, pen_translate) 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 cur_pen.x = pen.x cur_pen.y = pen.y - slot.bitmap_top * 64 self.draw_ft_bitmap(image, bitmap, cur_pen, color) 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 = (3, 3) text_size = 24 #ft = put_chinese_text('wqy-zenhei.ttc') ft = put_chinese_text('msyh.ttf') image = ft.draw_text(img, pos, line, text_size, color_) cv2.imshow('ss', image) cv2.waitKey(0)
相关文章推荐
- Freetype library not found问题详解,python(编程语言)+freetype(字体引擎)opencv 中文显示
- Opencv310图片Mat中叠加汉字(freetype+VS2015)
- 机器学习人脸检测和识别(中文标记姓名) python+opencv+freetype 图文教程和项目源代码
- PS去除图片中文字的方法详细图文教程
- 图像的读取显示以及保存----《opencv-python中文教程》
- 人脸检测和识别 源代码 下载-opencv3+python3.6完整实战项目源代码 识别视频《欢乐颂》中人物
- OpenCV 中文汉字显示
- OpenCV Python教程(3、直方图的计算与显示)
- 微软源代码管理工具TFS2013安装与使用详细图文教程(Vs2013)
- windows下python+flask环境配置详细图文教程
- iOS项目的完整重命名方法图文教程
- python图片加中文汉字水印
- jsp显示中文文件名的图片 详细出处参考:http://www.jb51.net/article/37149.htm
- iOS项目的完整重命名方法图文教程
- iOS开发-项目的完整重命名方法,图文教程。
- windows下python+flask环境配置详细图文教程
- OpenCV Python教程(3、直方图的计算与显示)
- SDL入门教程(八):2、裁剪精灵图片的完整源代码
- libGDX开发教程(三)--中文显示与汉字绘制
- XNA4.0 类和方法的注释显示为中文插件下载和安装方法(图文教程)