python中文字体下载_python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码...
效果展示
开发环境配置
python+freetype配置
项目完整文档下载(源代码+字体+图片)
完整字体下载
包括: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)
python中文字体下载_python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码...相关推荐
- python+freetype+opencv 图片中文(汉字)显示 详细图文教程和项目完整源代码
opencv图片写入中文(汉字)有两方法: 方法一: python+opencv+freetype https://blog.csdn.net/wyx100/article/details/75579 ...
- WindowsServer2012 R2 64位中文标准版(IIS8.5)下手动搭建PHP环境详细图文教程(二)安装IIS8.5...
//来源:http://www.imaoye.com/Technology/WindowsServer2012R264IIS85.html 阿里云服务器ECS Windows Server 2012 ...
- python中文字体下载_python docx 中文字体设置的操作方法
最近用到了docx生成word文档,docx本身用起来很方便,自带的各种样式都很好看,美中不足的就是对中文的支持不够好.在未设置中文字体的时候,生成的文档虽然可以显示中文,但是笔画大小不一,很难看. ...
- python中文字体下载_Python在Matplotlib图中显示中文字体的操作方法
1. 说明 本篇主要针对在Ubuntu系统中,matplotlib显示不了中文的问题,尤其是在无法安装系统字体的情况下,解决Python绘图时中文显示的问题. 2. 在系统中安装字体 $ fc-lis ...
- python中文字体下载_python中matlabplot和seaborn中文字体显示的一种解决方案
经过测试,该方法有效,分享一波,不多说了,直接上干货: 主要经过四步,比较永久的一种方法,适合需要经常显示中文标题 第一步:安装黑体SimHei.ttf 在这个百度云盘中下载: 链接:https:// ...
- windows 环境下(win10win7)Java8(jdk8)的下载+安装+环境变量设置全过程(超详细图文教程亲测版)
Java8 下载安装环境变量设置全过程 一.JDK下载 1.1 官网下载 1.2 下载成果 1.3 下载过程中的注意事项 二.JDK安装 三.环境变量配置 3.1 JDK在哪儿 3.2 环境变量在哪儿 ...
- 最好的中文字体下载站(带预览),1500个流行字体!
最好的中文字体下载站(带预览),1500个流行字体! http://www.xfbbs.com/Font/
- OpenCV图片动态特效显示(三)-- 平移显示及拉伸显示效果
学更好的别人, 做更好的自己. --<微卡智享> 本文长度为2927字,预计阅读8分钟 前言 前两篇的特效已经实现了展开.渐显及马赛克的实现,今天来实现图像的平移效果及通过显示窗体的函数改 ...
- VS2019配置opencv详细图文教程和测试代码
摘要: vs2019新鲜出炉,配置opencv又有哪些不一样呢,这个教程将会一步一步的教你如何配置opencv和跑动opencv一个简单的项目. 测试代码请在原文找到,转发备注原文链接:https:/ ...
最新文章
- android开发重要控件,Android界面编程——Android基本控件
- 那些年我们薅(撸)过的电子羊毛(设备)
- 编译原理题练习题测试题
- 音乐、视频播放模式切换实现方案及原理解析(基于vue、vuex、h5 audio)
- mysql bootstrap pxc_Mysql高可用之PXC
- linux java的启动内存溢出,java - Java的R代码可在Linux上运行,但不能在Windows中运行 - 堆栈内存溢出...
- 转:看问题的5个层次
- 为啥HashMap的默认容量是16?
- PyTorch学习(7)-Seq2Seq与 Attention
- 程序员面试强行用代码画画被骂,20分钟后面试官闭嘴了
- 大连理工大学计算机科学世界排名,喜忧参半!2021年5月ESI世界大学排行榜,21个学科排名数据分析!...
- 蓝桥杯日期填空问题:高斯日记/星系炸弹
- ORM框架,sqlsugar学习一
- 怎么使用systemctl启动rabbitmq_光纤激光切割机已经很久没有使用了。再次重新启动它,该怎么办?...
- 锐捷认证成功但是无法上网的问题
- oracle中ipad是什么意思,oracle 函数 Ipad的简单用法
- GifCam v7.0 极简GIF动画录制工具中文单文件版
- 边缘计算与智慧城市应用
- ValueError: Duplicate plugins for name projector
- SSH Remote forwarding的示例
热门文章
- java mina http_mina的HTTP性能测试
- 医院信息管理系统c语言代码大全,C语言程序设计医院信息管理系统附源代码.doc...
- 网络安全等级保护制度
- 如何利用长尾词引爆淘宝免费流量
- opcenter camstar ID生成规则
- 接口幂等性校验,太优雅了
- 下载超过 28762W 次的 Java面试题库(附答案)
- php两个相差为2的素数,重发系列证明之五:相差2k=6s+2的素数对是无穷多的
- 解决word删除mathtype后PPT里不停弹出关于mathtype的错误信息的问题
- 激励名言 摘抄其他编程人员的话