Python在圖片中插入大量文字并且自動(dòng)換行
問(wèn)題
如何在圖片中插入大量文字并且自動(dòng)換行
效果
原始圖
效果圖
注明
若需要寫入中文請(qǐng)使用中文字體
實(shí)現(xiàn)方式
from PIL import Image, ImageDraw, ImageFont class ImgText: font = ImageFont.truetype("micross.ttf", 24) def __init__(self, text): # 預(yù)設(shè)寬度 可以修改成你需要的圖片寬度 self.width = 100 # 文本 self.text = text # 段落 , 行數(shù), 行高 self.duanluo, self.note_height, self.line_height = self.split_text() def get_duanluo(self, text): txt = Image.new('RGBA', (100, 100), (255, 255, 255, 0)) draw = ImageDraw.Draw(txt) # 所有文字的段落 duanluo = "" # 寬度總和 sum_width = 0 # 幾行 line_count = 1 # 行高 line_height = 0 for char in text: width, height = draw.textsize(char, ImgText.font) sum_width += width if sum_width > self.width: # 超過(guò)預(yù)設(shè)寬度就修改段落 以及當(dāng)前行數(shù) line_count += 1 sum_width = 0 duanluo += '\n' duanluo += char line_height = max(height, line_height) if not duanluo.endswith('\n'): duanluo += '\n' return duanluo, line_height, line_count def split_text(self): # 按規(guī)定寬度分組 max_line_height, total_lines = 0, 0 allText = [] for text in self.text.split('\n'): duanluo, line_height, line_count = self.get_duanluo(text) max_line_height = max(line_height, max_line_height) total_lines += line_count allText.append((duanluo, line_count)) line_height = max_line_height total_height = total_lines * line_height return allText, total_height, line_height def draw_text(self): """ 繪圖以及文字 :return: """ note_img = Image.open("001.png").convert("RGBA") draw = ImageDraw.Draw(note_img) # 左上角開始 x, y = 0, 0 for duanluo, line_count in self.duanluo: draw.text((x, y), duanluo, fill=(255, 0, 0), font=ImgText.font) y += self.line_height * line_count note_img.save("result.png") if __name__ == '__main__': n = ImgText( "1234567890" * 5) n.draw_text()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Python3中的re.findall()方法及re.compile()
這篇文章主要介紹了Python3中的re.findall()方法及re.compile(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Python實(shí)現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法
這篇文章主要介紹了Python實(shí)現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法,涉及Python基于Linux平臺(tái)的字符串操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07使用Python實(shí)現(xiàn)合并多個(gè)Excel文件
合并Excel可以將多個(gè)文件中的數(shù)據(jù)合并到一個(gè)文件中,這樣可以幫助我們更好地匯總和管理數(shù)據(jù),本文主要介紹了如何使用第三方Python庫(kù) Spire.XLS for Python 實(shí)現(xiàn)以上兩種合并Excel文件的需求,有需要的可以了解下2023-12-12Python小工具之消耗系統(tǒng)指定大小內(nèi)存的方法
今天小編就為大家分享一篇Python小工具之消耗系統(tǒng)指定大小內(nèi)存的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之哈夫曼樹篇
這篇文章主要給大家介紹了關(guān)于Python描述數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之哈夫曼樹篇的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的相關(guān)資料,在Python中正則表達(dá)式是一種用于匹配和操作字符串的強(qiáng)大工具,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02Python實(shí)現(xiàn)進(jìn)程同步和通信的方法
本篇文章主要介紹了Python實(shí)現(xiàn)進(jìn)程同步和通信的方法,詳細(xì)的介紹了Process、Queue、Pipe、Lock等組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01