Python特效之文字成像方法詳解
一、特效預(yù)覽

處理前

處理后

細(xì)節(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('請(qǐng)輸入你想說的:') 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()到此這篇關(guān)于Python特效之文字成像方法詳解的文章就介紹到這了,更多相關(guān)Python文字成像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)戰(zhàn)爬蟲之女友欲買文胸不知何色更美
實(shí)踐來源于理論,做爬蟲前肯定要先了解相關(guān)的規(guī)則和原理,網(wǎng)絡(luò)爬蟲又稱為網(wǎng)頁(yè)蜘蛛,網(wǎng)絡(luò)機(jī)器人,更經(jīng)常的稱為網(wǎng)頁(yè)追逐者,是一種按照一定的規(guī)則,自動(dòng)地抓取萬維網(wǎng)信息的程序或者腳本。一句話概括就是網(wǎng)上信息搬運(yùn)工。本篇文章帶你深入了解,需要的朋友可以參考下2021-09-09
python按行讀取文件,去掉每行的換行符\n的實(shí)例
下面小編就為大家分享一篇python按行讀取文件,去掉每行的換行符\n的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
詳解python的webrtc庫(kù)實(shí)現(xiàn)語音端點(diǎn)檢測(cè)
這篇文章主要介紹了詳解python的webrtc庫(kù)實(shí)現(xiàn)語音端點(diǎn)檢測(cè),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析
這篇文章主要介紹了Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析,結(jié)合實(shí)例形式分析了Python浮點(diǎn)數(shù)操作的常見錯(cuò)誤,并簡(jiǎn)單解釋了浮點(diǎn)數(shù)運(yùn)算的原理與比較運(yùn)算實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
通過OpenCV實(shí)現(xiàn)對(duì)指定顏色的物體追蹤
這篇文章主要介紹的是通過OpenCV實(shí)現(xiàn)對(duì)特定顏色的物體追蹤,文中實(shí)驗(yàn)用的是綠蘿的樹葉。本文的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)OPenCV有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12

