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 相关文章推荐
Python 分析Nginx访问日志并保存到MySQL数据库实例
Mar 13 Python
Python使用logging结合decorator模式实现优化日志输出的方法
Apr 16 Python
python 巧用正则寻找字符串中的特定字符的位置方法
May 02 Python
利用python如何处理nc数据详解
May 23 Python
Python面向对象之继承和组合用法实例分析
Aug 27 Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 Python
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
Nov 29 Python
python运用sklearn实现KNN分类算法
Oct 16 Python
解决pycharm上的jupyter notebook端口被占用问题
Dec 17 Python
Python要求O(n)复杂度求无序列表中第K的大元素实例
Apr 02 Python
python实现简单遗传算法
Sep 18 Python
python 录制系统声音的示例
Dec 21 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
JavaScript null和undefined区别分析
2009/10/14 Javascript
用jQuery扩展自写的 UI导航
2010/01/13 Javascript
JS图片预加载 JS实现图片预加载应用
2012/12/03 Javascript
jquery+ajax+C#实现无刷新操作数据库数据的简单实例
2014/02/08 Javascript
Jquery幻灯片特效代码分享--打开页面随机选择切换方式(3)
2015/08/15 Javascript
JS+CSS实现简易实用的滑动门菜单效果
2015/09/18 Javascript
JS实现3D图片旋转展示效果代码
2015/09/22 Javascript
JavaScript对象数组排序函数及六个用法
2015/12/23 Javascript
JavaScript 控制字体大小设置的方法
2016/11/23 Javascript
Bootstrap进度条学习使用
2017/02/09 Javascript
vue.js事件处理器是什么
2017/03/20 Javascript
webpack的CSS加载器的使用
2018/09/11 Javascript
WebSocket的简单介绍及应用
2019/05/23 Javascript
JavaScript之数组扁平化详解
2019/06/03 Javascript
layui原生表单验证的实例
2019/09/09 Javascript
[03:00]《DAC最前线》之欧美新秀VS老将
2015/02/01 DOTA
python使用urllib模块和pyquery实现阿里巴巴排名查询
2014/01/16 Python
关于反爬虫的一些简单总结
2017/12/13 Python
python使用epoll实现服务端的方法
2018/10/16 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
2020/02/21 Python
快速解决jupyter notebook启动需要密码的问题
2020/04/21 Python
解决keras GAN训练是loss不发生变化,accuracy一直为0.5的问题
2020/07/02 Python
Python利用socket模块开发简单的端口扫描工具的实现
2021/01/27 Python
Python 转移文件至云对象存储的方法
2021/02/07 Python
HTML5 Video标签的属性、方法和事件汇总介绍
2015/04/24 HTML / CSS
12个不为大家熟知的HTML5设计小技巧
2016/06/02 HTML / CSS
应届大学生简历中的自我评价
2014/01/15 职场文书
咖啡厅创业计划书范本
2014/01/22 职场文书
党的群众路线教育实践活动公开承诺书
2014/03/28 职场文书
幼儿园开学寄语
2014/04/03 职场文书
体育运动口号
2014/06/09 职场文书
幼儿园感恩节活动总结
2015/03/24 职场文书
介绍信范文大全
2015/05/07 职场文书
回复函格式及范文
2015/07/14 职场文书
研究生毕业登记表的自我鉴定范文
2019/07/15 职场文书
5个实用的JavaScript新特性
2022/06/16 Javascript