Python特效之文字成像方法詳解
更新時間:2022年01月14日 11:17:30 作者:autofelix
文字成像,即將原圖片經過 python 處理后,生成完全由純文字組成的圖像。本文將具體為大家介紹如何通過Python實現(xiàn)這一效果,感興趣的可以學習一下
一、特效預覽
處理前
處理后
細節(jié)放大后
二、程序原理
1.輸入你想隱藏的文字
2.然后寫到另一張跟照片同等大小的空白紙張上
3.將相同位置的文字的顏色用照片上相同位置的顏色填充即可
4.然后生成新的圖片你聽懂了嗎
三、程序源碼
#!/usr/bin/env python # encoding: utf-8 from PIL import Image, ImageDraw, ImageFont class wordPicture: ''' This is a main Class, the file contains all documents. One document contains paragraphs that have several sentences It loads the original file and converts the original file to new content Then the new content will be saved by this class ''' def __init__(self): self.font_size = 7 self.picture = 'assets/picture.jpeg' def hello(self): ''' This is a welcome speech :return: self ''' print('*' * 50) print(' ' * 20 + '文字成像') print(' ' * 5 + 'Author: autofelix Date: 2022-01-06 13:14') print('*' * 50) return self def run(self): ''' The program entry ''' word = input('請輸入你想說的:') or '我鐘意你' resource = Image.open(self.picture) img_array = resource.load() image_new = Image.new('RGB', resource.size, (0, 0, 0)) draw = ImageDraw.Draw(image_new) font = ImageFont.truetype('/System/Library/Fonts/PingFang.ttc', self.font_size) yield_word = self.character_generator(word) for y in range(0, resource.size[1], self.font_size): for x in range(0, resource.size[0], self.font_size): draw.text((x, y), next(yield_word), font=font, fill=img_array[x, y], direction=None) image_new.convert('RGB').save('result.jpeg') def character_generator(self, text): while True: for i in range(len(text)): yield text[i] if __name__ == '__main__': wordPicture().hello().run()
到此這篇關于Python特效之文字成像方法詳解的文章就介紹到這了,更多相關Python文字成像內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼
ttkbootstrap 是一個基于 tkinter 的界面美化庫,使用這個工具可以開發(fā)出類似前端 bootstrap 風格的 tkinter 桌面程序。本文重點給大家介紹Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼,感興趣的朋友一起看看吧2022-02-02