python3.6环境下安装freetype库和基本使用方法(推荐)
作者:mrr 发布时间:2023-04-21 19:43:00
标签:python,安装,freetype
FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。在做图像展示的时候,可以写入中文文字,效果还是很好。
在之前安装库时基本都是直接切换到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)
总结


猜你喜欢
- 有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的。那么我们使用Python如何调用Linux的
- 使用 CAST 函数将 STRING 转为 BIGINT:SELECT CAST('00321' AS BIGINT) FR
- 最近老是要为现在这个项目初始化数据,搞的很头疼,而且数据库的Id自增越来越大,要让自增重新从1开始:那么就用下面的方法吧:方法一:如果曾经的
- 结合邮件告警和页面展示,再多的域名证书到期情况即可立马知道代码示例:# coding: utf-8 # 查询域名证书到期情况import r
- Django项目中为什么会加载静态时会失败呢?原因:django部署方式比较特别,采用静态文件路径:STATICFILES_DIRS的部署方
- 废话不多说了,直接给大家贴代码了,具体代码如下所示:// ----ajax begin $.ajax({type: "
- 目录一、常见orm数据库框架1、peewee 简单demo二、Model 和 Field 关系三、Model 模型四、Filed 字段1、字
- 本文实例讲述了Python列表生成式与生成器操作。分享给大家供大家参考,具体如下:列表生成式:能够用来创建list的生成式比如想要生成类似[
- 用Python编写过批量修改文件名的脚本程序,代码很简单,运行也比较快,唯一美中不足之处是每次批量修改文件名时都需要执行以下步骤:1)复制文
- 项目:基于Pymysql的专家随机抽取系统引入库函数:>>> import treelib>>> fro
- 1、概述python * 殊方法(魔术方法)是被python解释器调用的,我们自己不需要调用它们,我们统一使用内置函数来使用。例如:特殊方法_
- 数据库在高并发的场景下使用外键约束会有锁问题并且使用外键会增加运维成本,所以很多公司都规定生产环境的数据库禁止使用外键。那么不使用外键约束的
- 前言:我们知道,python代码文件大多数都是py类型。那么,能不能使用txt文件存储我们的代码呢?python这么强大的语言当然可以做大,
- 最近看JavaScript高级程序设计,大有收获,接下来几天写一下读书笔记。之前写了一篇Ajax初步理解的随笔,里面有个函数用来创建XmlH
- 一、软件包a) freetds-stable.gzb) php-5.2.12.tar.gz二、安装步骤a) tar zxvf freetds
- 下面一段代码给大家分享php未登录自动跳转到登录页面,具体代码如下所示:<?php namespace Home\Controller
- 1、存储过程基本语法: create procedure sp_name() begin ...... end; 2、如何调用: call
- python opencv实现目标跟踪python-opencv3.0新增了一些比较有用的 * 算法这里根据官网示例写了一个 * 类程序只能
- 如何用php实现APP消息推送现在有很多的消息推送厂商,比如阿里云的消息推送,极光推送,融云的消息推送。他们的原理都是把sdk内置在app里
- 1、前言在Python中元组是一个和列表非常类似的数据类型,不同之处就是列表中的元素可以修改,而元组之中的元素不可以修改。2、定义和使用元组