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 相关文章推荐
web.py在SAE中的Session问题解决方法(使用mysql存储)
Jun 24 Python
用不到50行的Python代码构建最小的区块链
Nov 16 Python
Python3.6笔记之将程序运行结果输出到文件的方法
Apr 22 Python
python 将列表中的字符串连接成一个长路径的方法
Oct 23 Python
Windows 8.1 64bit下搭建 Scrapy 0.22 环境
Nov 18 Python
PyQt5组件读取参数的实例
Jun 25 Python
详解Python 中sys.stdin.readline()的用法
Sep 12 Python
python3操作注册表的方法(Url protocol)
Feb 05 Python
Python通过Pillow实现图片对比
Apr 29 Python
python小程序之4名牌手洗牌发牌问题解析
May 15 Python
python判断正负数方式
Jun 03 Python
PyCharm2020.1.1与Python3.7.7的安装教程图文详解
Aug 07 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
收音机另类DIY - 纸巾盒做外壳
2021/03/02 无线电
php 基础函数
2017/02/10 PHP
Prototype源码浅析 Enumerable部分(二)
2012/01/18 Javascript
JavaScript 参数中的数组展开 [译]
2012/09/21 Javascript
JQuery处理json与ajax返回JSON实例代码
2014/01/03 Javascript
浅谈javascript中for in 和 for each in的区别
2015/04/23 Javascript
JavaScript中使用concat()方法拼接字符串的教程
2015/06/06 Javascript
原生js实现autocomplete插件
2016/04/14 Javascript
浅析JavaScript 箭头函数 generator Date JSON
2016/05/23 Javascript
jQuery改变form表单的action,并进行提交的实现代码
2016/05/25 Javascript
JS弹性运动实现方法分析
2016/12/15 Javascript
使用gulp搭建本地服务器并实现模拟ajax
2017/04/05 Javascript
jQuery条件分页 代替离线查询(附代码)
2017/08/17 jQuery
JavaScript requestAnimationFrame动画详解
2017/09/14 Javascript
vue init webpack myproject构建项目 ip不能访问的解决方法
2018/03/20 Javascript
JavaScript生成一个不重复的ID的方法示例
2019/09/16 Javascript
vue.js自定义组件实现v-model双向数据绑定的示例代码
2020/01/08 Javascript
详解JavaScript 高阶函数
2020/09/14 Javascript
使用webpack5从0到1搭建一个react项目的实现步骤
2020/12/16 Javascript
Python下载网络文本数据到本地内存的四种实现方法示例
2018/02/05 Python
初探TensorFLow从文件读取图片的四种方式
2018/02/06 Python
Python实现银行账户资金交易管理系统
2020/01/03 Python
Django实现后台上传并显示图片功能
2020/05/29 Python
台湾旅游网站:雄狮旅游网
2017/08/16 全球购物
上海天奕面试题笔试题
2015/04/19 面试题
毕业晚会主持词
2014/03/24 职场文书
2014年创先争优活动总结
2014/05/04 职场文书
党员领导干部承诺书
2014/05/28 职场文书
党员干部廉洁承诺书
2014/05/28 职场文书
七一党日活动总结
2014/07/08 职场文书
聚会通知怎么写
2015/04/23 职场文书
2015年保洁员工作总结
2015/05/04 职场文书
react如何快速设置文件路径别名
2021/04/28 Javascript
浅谈MySQL next-key lock 加锁范围
2021/06/07 MySQL
Golang中异常处理机制详解
2021/06/08 Golang
MySQL实现用逗号进行拼接、以逗号进行分割
2022/12/24 MySQL