實(shí)戰(zhàn)!用 Python 給母親送祝福!

文 | 潮汐
來源:Python 技術(shù)「ID: pythonall」

今天是母親節(jié),小阿醬在這里祝天下所有的母親節(jié)日快樂,作為女兒的我除了買禮物送驚喜外還要用 Python 送上特殊的祝福!
母親節(jié)(Mother’s Day),是一個(gè)感謝母親的節(jié)日。媽媽曾經(jīng)也是一個(gè)女孩子,怕黑怕蟲子,也會(huì)掉眼淚,笨手笨腳怕扎針,但她溫柔了我,溫柔了歲月。
借此祝全天下媽媽母親節(jié)快樂!
制作母親節(jié)詞云圖-祝福方式1
這個(gè)世界只有一個(gè)母親,包容我的一千萬任性!
今天我用 Python 制作一個(gè)詞云圖給媽媽送祝福,制作代碼如下:
import numpy
import multidict
import matplotlib.pyplot as plt
from scipy.misc import imread
from wordcloud import WordCloud, ImageColorGenerator
def transform_format(val):
"""
用于去除雜色
Arguments:
val {[array]} -- RGB顏色組
Returns:
[array] -- 去除雜色后的值
"""
if val[0] > 245 and val[1] > 245 and val[2] > 245:
val[0] = val[1] = val[2] = 255
return val
else:
return val
def gen_happy_birthday_cloud(file, name):
words = multidict.MultiDict()
# 必須先初始化兩個(gè)最大權(quán)重的
words.add('母親節(jié)快樂', 10)
words.add(name, 12)
# 隨意插入新的詞語
for i in range(1000):
words.add('媽媽', numpy.random.randint(1, 5))
words.add('您辛苦了', numpy.random.randint(1, 5))
words.add(name, numpy.random.randint(1, 5))
# 設(shè)定圖片
bimg = imread(file)
for color in range(len(bimg)):
bimg[color] = list(map(transform_format, bimg[color]))
wordcloud = WordCloud(
background_color='white',
mask=bimg,
font_path='simhei.ttf'
).generate_from_frequencies(words)
# 生成詞云
bimgColors = ImageColorGenerator(bimg)
# 渲染詞云
plt.axis("off")
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.savefig(name + '.png')
plt.show()
gen_happy_birthday_cloud("mother.jpg", "母親節(jié)快樂")
思路為: 導(dǎo)入一張圖片后再輸入節(jié)日祝福語后進(jìn)行圖片渲染,最后再根據(jù)圖片形狀生成相應(yīng)詞云圖。
運(yùn)行結(jié)果如下:
另一個(gè)形狀圖片生成的詞云圖:
飄落愛心玫瑰-祝福方式2
接下來還要制作一個(gè)唯美飄落桃心給媽媽們送祝福!
import time
from random import randint
for i in range(1, 35): # 打印抬頭
print('')
heartStars = [2, 4, 8, 10, 14, 20, 26, 28, 40, 44, 52, 60, 64, 76] # *的位置
heartBreakLines = [13, 27, 41, 55, 69, 77] # 空格的位置
flowerBreakLines = [7, 15, 23, 31, 39, 46] # 玫瑰的空列位置
def addSpaces(a): # 添加空列
count = a
while count > 0:
print(' ', end='')
count -= 1
def newLineWithSleep(): # 添加空行
time.sleep(0.3)
print('\n', end='')
play = 0
while play == 0:
Left_Spaces = randint(8, 80)
addSpaces(Left_Spaces)
for i in range(0, 78): # 比心的形狀
if i in heartBreakLines:
newLineWithSleep()
addSpaces(Left_Spaces)
elif i in heartStars:
print('*', end='')
elif i in (32, 36):
print('M', end='')
elif i == 34:
print('O', end='')
else:
print(' ', end='')
newLineWithSleep()
addSpaces(randint(8, 80))
print("H a p p y M o t h e r ' s D a y !", end='')
newLineWithSleep()
newLineWithSleep()
Left_Spaces = randint(8, 80)
addSpaces(Left_Spaces)
for i in range(0, 47): # 向母親獻(xiàn)花
if i in flowerBreakLines:
newLineWithSleep()
addSpaces(Left_Spaces)
elif i in (2, 8, 12, 18):
print('{', end='')
elif i in (3, 9, 13, 19):
print('_', end='')
elif i in (4, 10, 14, 20):
print('}', end='')
elif i in (27, 35, 43):
print('|', end='')
elif i in (34, 44):
print('~', end='')
elif i == 11:
print('o', end='')
else:
print(' ', end='')
print('\n', end='')
實(shí)現(xiàn)效果如下:
總結(jié)
今天是母親節(jié),借此希望大家能夠好好陪陪父母,或者給母親送上一份祝福和好禮。也希望今天的文章對(duì)大家有幫助。
寫在最后
親愛的媽媽,其實(shí)我愛你要比你愛我久一點(diǎn),你從 20 歲就開始愛我,而我一出生就開始愛你,你只能愛我?guī)资辏覑勰阋惠呑樱?/p>
歲月從不敗美人,祝全天下媽媽母親節(jié)快樂!
PS:公號(hào)內(nèi)回復(fù)「Python」即可進(jìn)入Python 新手學(xué)習(xí)交流群,一起 100 天計(jì)劃!
老規(guī)矩,兄弟們還記得么,右下角的 “在看” 點(diǎn)一下,如果感覺文章內(nèi)容不錯(cuò)的話,記得分享朋友圈讓更多的人知道!


【代碼獲取方式】
