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交换变量
Sep 06 Python
详解Python中的装饰器、闭包和functools的教程
Apr 02 Python
python利用不到一百行代码实现一个小siri
Mar 02 Python
django基础之数据库操作方法(详解)
May 24 Python
Python使用Matplotlib实现Logos设计代码
Dec 25 Python
基于windows下pip安装python模块时报错总结
Jun 12 Python
python3 打开外部程序及关闭的示例
Nov 06 Python
python实现串口自动触发工作的示例
Jul 02 Python
TensorFLow 变量命名空间实例
Feb 11 Python
解决python3插入mysql时内容带有引号的问题
Mar 02 Python
python torch.utils.data.DataLoader使用方法
Apr 02 Python
Python利用socket模块开发简单的端口扫描工具的实现
Jan 27 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
php数据库操作model类(使用__call方法)
2016/11/16 PHP
PHP实现一个限制实例化次数的类示例
2019/09/16 PHP
用javascript实现画板的代码
2007/09/05 Javascript
各种效果的jquery ui(接口)介绍
2008/09/17 Javascript
获取焦点时,利用js定时器设定时间执行动作
2010/04/02 Javascript
基于jQuery判断两个元素是否有重叠部分的代码
2012/07/25 Javascript
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
java与javascript之间json格式数据互转介绍
2013/10/29 Javascript
js style动态设置table高度
2014/10/21 Javascript
又一枚精彩的弹幕效果jQuery实现
2016/07/25 Javascript
微信小程序 框架详解及实例应用
2016/09/26 Javascript
JavaScript中省略元素对数组长度的影响
2016/10/26 Javascript
vue中如何使用ztree
2018/02/06 Javascript
mpvue小程序仿qq左滑置顶删除组件
2018/08/03 Javascript
vue自定义指令实现方法详解
2019/02/11 Javascript
js图片无缝滚动插件使用详解
2020/05/26 Javascript
python基础教程之实现石头剪刀布游戏示例
2014/02/11 Python
Python复制目录结构脚本代码分享
2015/03/06 Python
python通过yield实现数组全排列的方法
2015/03/18 Python
Python使用Matplotlib实现雨点图动画效果的方法
2017/12/23 Python
python构建深度神经网络(续)
2018/03/10 Python
对python pandas读取剪贴板内容的方法详解
2019/01/24 Python
python opencv 批量改变图片的尺寸大小的方法
2019/06/28 Python
MAC平台基于Python Appium环境搭建过程图解
2020/08/13 Python
python 实现有道翻译功能
2021/02/26 Python
html5 css3 动态气泡按钮实例演示
2012/12/02 HTML / CSS
SKECHERS官方旗舰店:美国舒适运动休闲品牌
2017/12/22 全球购物
英国时尚女装购物网站:Missguided
2018/08/23 全球购物
New Balance澳大利亚官网:运动鞋和健身服装
2019/02/23 全球购物
伦敦著名的运动鞋综合商店:Footpatrol
2019/03/25 全球购物
印尼第一大家居、生活和家具电子商务:Ruparupa
2019/11/25 全球购物
伊莱克斯(Electrolux)俄罗斯网上商店:瑞典家用电器品牌
2021/01/23 全球购物
在校生钳工实习自我鉴定
2013/09/19 职场文书
公司踏青活动方案
2014/08/16 职场文书
教师师德师风自我剖析材料
2014/09/29 职场文书
群众路线查摆问题整改措施思想汇报
2014/10/10 职场文书