Skip to content

Commit 2484220

Browse files
committed
submit code
submit code
1 parent 222cfce commit 2484220

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import numpy
2+
import multidict
3+
import matplotlib.pyplot as plt
4+
from scipy.misc import imread
5+
from wordcloud import WordCloud, ImageColorGenerator
6+
7+
def transform_format(val):
8+
"""
9+
用于去除杂色
10+
Arguments:
11+
val {[array]} -- RGB颜色组
12+
Returns:
13+
[array] -- 去除杂色后的值
14+
"""
15+
if val[0] > 245 and val[1] > 245 and val[2] > 245:
16+
val[0] = val[1] = val[2] = 255
17+
return val
18+
else:
19+
return val
20+
21+
22+
def gen_happy_birthday_cloud(file, name):
23+
words = multidict.MultiDict()
24+
# 必须先初始化两个最大权重的
25+
words.add('母亲节快乐', 12)
26+
words.add(name, 10)
27+
28+
# 随意插入新的词语
29+
for i in range(1000):
30+
words.add('亲爱的妈妈', numpy.random.randint(1, 5))
31+
words.add('您辛苦了', numpy.random.randint(1, 5))
32+
words.add(name, numpy.random.randint(1, 5))
33+
34+
# 设定图片
35+
bimg = imread(file)
36+
for color in range(len(bimg)):
37+
bimg[color] = list(map(transform_format, bimg[color]))
38+
39+
wordcloud = WordCloud(
40+
background_color='pink',
41+
mask=bimg,
42+
font_path='simhei.ttf'
43+
).generate_from_frequencies(words)
44+
45+
# 生成词云
46+
bimgColors = ImageColorGenerator(bimg)
47+
48+
# 渲染词云
49+
plt.axis("off")
50+
plt.imshow(wordcloud.recolor(color_func=bimgColors))
51+
plt.savefig(name + '.png')
52+
plt.show()
53+
54+
gen_happy_birthday_cloud("mother1.jpg", "母亲节")

chaoxi/mother's_day/mother.jpg

13.2 KB
Loading

chaoxi/mother's_day/mother1.jpg

24.1 KB
Loading

chaoxi/mother's_day/mother_love.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import time
2+
from random import randint
3+
4+
for i in range(1, 35): # 打印抬头
5+
print('')
6+
7+
heartStars = [2, 4, 8, 10, 14, 20, 26, 28, 40, 44, 52, 60, 64, 76] # *的位置
8+
heartBreakLines = [13, 27, 41, 55, 69, 77] # 空格的位置
9+
flowerBreakLines = [7, 15, 23, 31, 39, 46] # 玫瑰的空列位置
10+
11+
def addSpaces(a): # 添加空列
12+
count = a
13+
while count > 0:
14+
print(' ', end='')
15+
count -= 1
16+
17+
18+
def newLineWithSleep(): # 添加空行
19+
time.sleep(0.3)
20+
print('\n', end='')
21+
22+
23+
play = 0
24+
while play == 0:
25+
Left_Spaces = randint(8, 80)
26+
addSpaces(Left_Spaces)
27+
28+
for i in range(0, 78): # 比心的形状
29+
if i in heartBreakLines:
30+
newLineWithSleep()
31+
addSpaces(Left_Spaces)
32+
elif i in heartStars:
33+
print('*', end='')
34+
elif i in (32, 36):
35+
print('M', end='')
36+
elif i == 34:
37+
print('O', end='')
38+
else:
39+
print(' ', end='')
40+
41+
newLineWithSleep()
42+
addSpaces(randint(8, 80))
43+
print("H a p p y M o t h e r ' s D a y !", end='')
44+
newLineWithSleep()
45+
newLineWithSleep()
46+
47+
Left_Spaces = randint(8, 80)
48+
addSpaces(Left_Spaces)
49+
for i in range(0, 47): # 向母亲献花
50+
if i in flowerBreakLines:
51+
newLineWithSleep()
52+
addSpaces(Left_Spaces)
53+
elif i in (2, 8, 12, 18):
54+
print('{', end='')
55+
elif i in (3, 9, 13, 19):
56+
print('_', end='')
57+
elif i in (4, 10, 14, 20):
58+
print('}', end='')
59+
elif i in (27, 35, 43):
60+
print('|', end='')
61+
elif i in (34, 44):
62+
print('~', end='')
63+
elif i == 11:
64+
print('o', end='')
65+
else:
66+
print(' ', end='')
67+
68+
print('\n', end='')

0 commit comments

Comments
 (0)