python3.6环境下安装freetype库和基本使用方法(推荐)

作者:mrr 时间:2023-04-21 19:43:00 

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)

总结

标签:python,安装,freetype
0
投稿

猜你喜欢

  • python实现远程控制电脑

    2022-12-07 21:00:16
  • python实现图像识别功能

    2023-08-09 08:45:00
  • python map比for循环快在哪

    2021-06-16 09:39:04
  • SQLServer查找字符串在另一字符串的索引位置

    2024-01-13 06:10:30
  • python3 拼接字符串的7种方法

    2021-12-24 09:09:32
  • python使用KNN算法识别手写数字

    2022-02-20 10:48:23
  • Python用内置模块来构建REST服务与RPC服务实战

    2023-05-09 19:32:16
  • MySQL执行update语句和原数据相同会再次执行吗

    2024-01-20 16:49:31
  • PyQt5每天必学之组合框

    2023-08-30 07:55:06
  • Python第三方包PrettyTable安装及用法解析

    2023-02-22 03:19:16
  • vue2 vue3中使用Echarts详细

    2024-05-09 15:23:37
  • php删除二维数组中的重复值方法

    2024-06-05 09:51:20
  • python爬虫如何解决图片验证码

    2021-04-27 11:58:57
  • Python实现简单层次聚类算法以及可视化

    2023-09-04 00:16:21
  • python实现在函数中修改变量值的方法

    2023-12-15 04:15:02
  • Python区块链创建Genesis Block教程

    2022-04-17 10:44:15
  • Python设计模式编程中的备忘录模式与对象池模式示例

    2023-02-06 05:48:43
  • pandas时间序列之如何将int转换成datetime格式

    2023-03-09 07:50:34
  • 简单易懂Pytorch实战实例VGG深度网络

    2021-09-07 19:47:24
  • Python实现FTP文件传输的实例

    2021-12-16 02:35:31
  • asp之家 网络编程 m.aspxhome.com