python3.6环境下安装freetype库和基本使用方法(推荐)


Posted in Python onMay 10, 2020

FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。在做图像展示的时候,可以写入中文文字,效果还是很好。

python3.6环境下安装freetype库和基本使用方法(推荐)

在之前安装库时基本都是直接切换到python3.6环境下直接pip install XXX,在安装freetype直接pip install freetype不可以了,查了半天又是编译又是官网下载的,太麻烦,不推荐。

(1)正确的安装方法:
注意:一定要加上 -py

pip install freetype-py

(2)常用调用方法

已经封装好了一个文件,可直接保存后调用。

import freetype
import copy


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)

  text = text
  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) * 0x10000, int(0.2 * 0x10000), \
         int(0.0 * 0x10000), int(1.1 * 0x10000))
  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:
     try:
      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]
     except:
      continue


if __name__ == '__main__':
 # just for test
 import cv2

 line = '毛不易'
 img = cv2.imread('./aa.jpg')

 color_ = (0, 255, 0) # Green
 pos = (3, 3)
 text_size = 24
 ft = put_chinese_text('yahei.ttf')
 image = ft.draw_text(img, pos, line, text_size, color_)

 cv2.imshow('ss', image)
 cv2.waitKey(0)

总结

到此这篇关于python3.6环境下安装freetype库和基本使用方法(推荐)的文章就介绍到这了,更多相关python3.6安装freetype库内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
利用soaplib搭建webservice详细步骤和实例代码
Nov 20 Python
python对指定目录下文件进行批量重命名的方法
Apr 18 Python
python实现在每个独立进程中运行一个函数的方法
Apr 23 Python
使用rpclib进行Python网络编程时的注释问题
May 06 Python
Python实现多线程抓取网页功能实例详解
Jun 08 Python
python-opencv在有噪音的情况下提取图像的轮廓实例
Aug 30 Python
Django中redis的使用方法(包括安装、配置、启动)
Feb 21 Python
python3 面向对象__类的内置属性与方法的实例代码
Nov 09 Python
Python中类的创建和实例化操作示例
Feb 27 Python
Python中的 ansible 动态Inventory 脚本
Jan 19 Python
python进度条显示-tqmd模块的实现示例
Aug 23 Python
python中filter,map,reduce的作用
Jun 10 Python
Pycharm中安装wordcloud等库失败问题及终端通过pip安装的Python库如何添加到Pycharm解释器中(推荐)
May 10 #Python
python对接ihuyi实现短信验证码发送
May 10 #Python
python调用API接口实现登陆短信验证
May 10 #Python
aws 通过boto3 python脚本打pach的实现方法
May 10 #Python
Django 设置admin后台表和App(应用)为中文名的操作方法
May 10 #Python
基于python实现上传文件到OSS代码实例
May 09 #Python
使用python创建生成动态链接库dll的方法
May 09 #Python
You might like
Zend Framework教程之Application用法实例详解
2016/03/14 PHP
php中二分法查找算法实例分析
2016/09/22 PHP
PHP中for循环与foreach的区别
2017/03/06 PHP
JavaScript 布尔操作符解析  &amp;&amp; || !
2012/08/10 Javascript
JS仿Windows开机启动Loading进度条的方法
2015/02/26 Javascript
JSONP之我见
2015/03/24 Javascript
jQuery获得包含margin的outerWidth和outerHeight的方法
2015/03/25 Javascript
浅谈js里面的InttoStr和StrtoInt
2016/06/14 Javascript
js style.display=block显示布局错乱问题的解决方法
2016/09/21 Javascript
Jquery实现跨域异步上传文件总结
2017/02/03 Javascript
extjs简介_动力节点Java学院整理
2017/07/17 Javascript
浅谈vuepress 踩坑记
2018/04/18 Javascript
Vue中控制v-for循环次数的实现方法
2018/09/26 Javascript
Vuex 模块化使用详解
2019/07/31 Javascript
Node.js使用MongoDB的ObjectId作为查询条件的方法
2019/09/10 Javascript
Element DateTimePicker日期时间选择器的使用示例
2020/07/27 Javascript
微信小程序实现点击生成随机验证码
2020/09/09 Javascript
python自动化测试之setUp与tearDown实例
2014/09/28 Python
Python程序员面试题 你必须提前准备!(答案及解析)
2018/01/23 Python
python使用matplotlib库生成随机漫步图
2018/08/27 Python
浅谈Pandas:Series和DataFrame间的算术元素
2018/12/22 Python
利用Python半自动化生成Nessus报告的方法
2019/03/19 Python
Python2与Python3的区别实例总结
2019/04/17 Python
Python学习之time模块的基本使用
2021/01/17 Python
沙特阿拉伯排名第一的在线时尚购物应用程序:1Zillion
2020/08/08 全球购物
C#公司笔试题
2014/03/28 面试题
某公司C#程序员面试题笔试题
2014/05/26 面试题
学生打架检讨书大全
2014/01/23 职场文书
优秀大学生职业生涯规划书
2014/02/27 职场文书
小学生操行评语
2014/04/22 职场文书
2015年社区流动人口工作总结
2015/05/12 职场文书
2016年国庆节新闻稿范文
2015/11/25 职场文书
2016年教师党员创先争优承诺书
2016/03/24 职场文书
家电创业计划书
2019/08/05 职场文书
python用tkinter开发的扫雷游戏
2021/06/01 Python
解决SpringCloud Feign传对象参数调用失败的问题
2021/06/23 Java/Android