av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

python3.6環境下安裝freetype庫和基本使用方法(推薦)

瀏覽:34日期:2022-07-26 09:04:05

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 freetypeimport copyclass 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: continueif __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 編程
相關文章:
主站蜘蛛池模板: 91久久| 欧美成人第一页 | 男女视频在线免费观看 | 神马久久久久久久久久 | 国产欧美一区二区精品久导航 | 91在线一区 | 黄色亚洲 | 国精产品一区二区三区 | 91精品久久久 | 亚洲视频欧美视频 | 国产伦精品一区二区三区视频金莲 | 国产美女精品 | 男女视频在线免费观看 | 亚洲天堂男人的天堂 | 精品一区二区三区中文字幕 | 欧美h视频| 日韩国产高清在线观看 | 91麻豆精品国产91久久久更新资源速度超快 | 久久天天躁狠狠躁夜夜躁2014 | 美女福利网站 | 久久乐国产精品 | 日韩一区二区三区在线观看 | 国产男女精品 | 美女一区 | 91福利网址 | 超碰8 | 在线播放中文 | 天堂在线1 | 欧美色成人 | 国产精品久久久久久久午夜片 | 精品一区二区三区入口 | 成人无遮挡毛片免费看 | 欧美大片黄 | 亚洲欧洲精品在线 | 欧美久久久久久久久 | 国产免费又色又爽又黄在线观看 | 久久伊人影院 | 在线免费观看欧美 | 中文字幕一区二区三区四区五区 | 久热免费 | 正在播放国产精品 |