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中让MySQL查询结果返回字典类型的方法
Aug 22 Python
python实现的文件夹清理程序分享
Nov 22 Python
在Python中使用next()方法操作文件的教程
May 24 Python
解析Python编程中的包结构
Oct 25 Python
Python 用Redis简单实现分布式爬虫的方法
Nov 23 Python
Python实现简易版的Web服务器(推荐)
Jan 29 Python
超简单使用Python换脸实例
Mar 27 Python
Python爬虫使用代理IP的实现
Oct 27 Python
python3 实现调用串口功能
Dec 26 Python
Python错误的处理方法
Jun 23 Python
Pandas中DataFrame基本函数整理(小结)
Jul 20 Python
Ubuntu16安装Python3.9的实现步骤
Dec 15 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操作XML作为数据库的类
2010/12/19 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
2011/06/16 PHP
phpmyadmin出现Cannot start session without errors问题解决方法
2014/08/14 PHP
PHP使用静态方法的几个注意事项
2014/09/16 PHP
Ubuntu中搭建Nginx、PHP环境最简单的方法
2015/03/05 PHP
ThinkPHP5.1验证码功能实现的示例代码
2020/06/08 PHP
弹出窗口并且此窗口带有半透明的遮罩层效果
2014/03/13 Javascript
Ajax清除浏览器js、css、图片缓存的方法
2015/08/06 Javascript
js实现简单计算器
2015/11/22 Javascript
jQuery绑定事件监听bind和移除事件监听unbind用法实例详解
2016/01/19 Javascript
js获取新浪天气接口的实现代码
2016/06/06 Javascript
jQuery插件FusionCharts绘制的2D帕累托图效果示例【附demo源码】
2017/03/28 jQuery
layui弹出层效果实现代码
2017/05/19 Javascript
vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法
2017/11/28 Javascript
JS实现数组去重及数组内对象去重功能示例
2019/02/02 Javascript
vue 组件简介
2020/07/31 Javascript
VUE : vue-cli中去掉路由中的井号#操作
2020/09/04 Javascript
[01:46]2018完美盛典章节片——坚守
2018/12/17 DOTA
python从ftp下载数据保存实例
2013/11/20 Python
Python网络编程使用select实现socket全双工异步通信功能示例
2018/04/09 Python
python读文件保存到字典,修改字典并写入新文件的实例
2018/04/23 Python
Python 网络爬虫--关于简单的模拟登录实例讲解
2018/06/01 Python
Django+Xadmin构建项目的方法步骤
2019/03/06 Python
Python生成指定数量的优惠码实操内容
2019/06/18 Python
导入tensorflow时报错:cannot import name 'abs'的解决
2019/10/10 Python
美国礼品卡交易网站:Cardpool
2018/08/27 全球购物
英国当代时尚和街头服饰店:18montrose
2018/12/15 全球购物
Under Armour安德玛荷兰官网:美国高端运动科技品牌
2019/07/10 全球购物
英国水族馆和池塘用品购物网站:Warehouse Aquatics
2019/08/29 全球购物
求职简历中自我评价
2014/01/28 职场文书
甜美蛋糕店创业计划书
2014/01/30 职场文书
村级环境卫生整治方案
2014/05/04 职场文书
反腐倡廉演讲稿
2014/05/22 职场文书
小学生通知书评语
2014/12/31 职场文书
MySQL系列之十三 MySQL的复制
2021/07/02 MySQL
优化Mysql查询的示例
2022/04/26 MySQL