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)
总结
0
投稿
猜你喜欢
- 学习https://matplotlib.org/gallery/index.html 记录,描述不一定准确,具体请参考官网Matplotl
- 通过PyFetion可以很方便的使用python来用飞信发送短信,下面贴出代码:from fetion import *import osd
- display_errors 错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项。错误回显可以暴露出非常多的敏感信息,
- 表格制作好了,内容也有了,怎么看着别扭呀!哦,还没有给表格化化装--格式化表格呀,俗话说:人靠衣服也靠鞍,要想让你制作的网页漂漂亮亮、美观大
- 一、Tesseract简介Tesseract是一个OCR库(OCR是英文Optical Character Recognition的缩写),
- 目录总体思路:判断链接是否指向文件:下载文件:获取 url 下的所有链接:最近维基 jie mi 彻底公开了网站的全部文件,我就在想如何使用
- 本文先比较range与arange的异同点,再详细介绍各自的用法,然后列举了几个简单的示例,最后对xrange进行了简单的说明。1. ran
- .tkintertkinter是Python下面向tk的图形界面接口库,可以方便地进行图形界面设计和交互操作编程。tkinter的优点是简单
- 实验环境:tensorflow版本1.2.0,python2.7介绍惯例先展示函数:tf.nn.conv2d(input, filter,
- <script> Function.prototype.$bind=function(object) {  
- 一、Python 的 IDE —— PyCharm1.1 集成开发环境(IDE)集成开发环境(IDE,Integrated Developm
- 在python中启动和关闭线程:首先导入threadingimport threading然后定义一个方法def serial_read()
- 前不久网上公开了一个MySQL Func的漏洞,讲的是使用MySQL创建一个自定义的函数,然后通过这个函数来攻击服务器。最早看到相关的报道是
- 很常见的一个图片轮播Flash,使用之后发现在IE下按F5刷新之后Flash区域就变成一大块背景色,内容轮播出不来了。有趣的是右键点击Fla
- 经纬度坐标转换最常见办法就是调用第三方 API,例如百度、高德地图等服务平台,提供了相应的功能接口,它们的这类技术已经非常成熟啦,准确稳定,
- FLV在线转换,是目前主流播客网上通用的一种视频解决方案需要用到的组件 ASPExecmencoderffmpeg.exe第一步骤: 在线转
- Background之前数据库只区分了Android,IOS两个平台,游戏上线后现在PM想要区分国服,海外服,港台服。这几个字段从前端那里的
- 参数数量及其作用该函数共有两个参数,分别是key和scope。def get_collection(key, scope=None) Wra
- 本文介绍了redis之django-redis的简单缓存使用,分享给大家,具体如下:自定义连接池这种方式跟普通py文件操作redis一样,代
- 本文实例讲述了Django中使用group_by的方法。分享给大家供大家参考。具体分析如下:在Django中怎样使用group_by语句呢?