python3.6環(huán)境下安裝freetype庫(kù)和基本使用方法(推薦)
FreeType庫(kù)是一個(gè)完全免費(fèi)(開(kāi)源)的、高質(zhì)量的且可移植的字體引擎,它提供統(tǒng)一的接口來(lái)訪問(wèn)多種字體格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。在做圖像展示的時(shí)候,可以寫(xiě)入中文文字,效果還是很好。
在之前安裝庫(kù)時(shí)基本都是直接切換到python3.6環(huán)境下直接pip install XXX,在安裝freetype直接pip install freetype
不可以了,查了半天又是編譯又是官網(wǎng)下載的,太麻煩,不推薦。
(1)正確的安裝方法:
注意:一定要加上 -py
pip install freetype-py
(2)常用調(diào)用方法
已經(jīng)封裝好了一個(gè)文件,可直接保存后調(diào)用。
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)
總結(jié)
到此這篇關(guān)于python3.6環(huán)境下安裝freetype庫(kù)和基本使用方法(推薦)的文章就介紹到這了,更多相關(guān)python3.6安裝freetype庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)web.py設(shè)置favicon.ico的方法詳解
今天小編就為大家分享一篇對(duì)web.py設(shè)置favicon.ico的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能
這篇文章主要介紹了Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能,簡(jiǎn)單介紹了progressbar模塊的安裝,并結(jié)合實(shí)例形式分析了Python使用progressbar模塊顯示進(jìn)度條的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python利用PyExecJS庫(kù)執(zhí)行JS函數(shù)的案例分析
這篇文章主要介紹了Python利用PyExecJS庫(kù)執(zhí)行JS函數(shù),本文通過(guò)案例分析給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Python查找算法之折半查找算法的實(shí)現(xiàn)
這篇文章主要介紹了Python查找算法之折半查找算法的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Django模塊學(xué)習(xí)之模塊語(yǔ)言詳解
模板語(yǔ)言渲染的整個(gè)過(guò)程其實(shí)就是將html轉(zhuǎn)換成函數(shù),并為該函數(shù)提供全局變量,然后執(zhí)行該函數(shù),下面這篇文章主要給大家介紹了關(guān)于Django模塊學(xué)習(xí)之模塊語(yǔ)言的相關(guān)資料,需要的朋友可以參考下2021-11-11Python序列化基礎(chǔ)知識(shí)(json/pickle)
這篇文章主要為大家詳細(xì)介紹了Python序列化json和pickle基礎(chǔ)知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10django admin添加數(shù)據(jù)自動(dòng)記錄user到表中的實(shí)現(xiàn)方法
下面小編就為大家分享一篇django admin添加數(shù)據(jù)自動(dòng)記錄user到表中的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01教你學(xué)會(huì)通過(guò)python的matplotlib庫(kù)繪圖
今天教大家如何學(xué)會(huì)通過(guò)python的matplotlib庫(kù)繪圖,文中有非常詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05