一键生成漂亮的节日快乐词云图,给女神一个特别的礼物~
# Python 实用宝典
# 2020/03/23
先看效果图:
代码传送门:
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()
# 节日快乐和姓名的权重必须先初始化两个最大权重的
words.add('节日快乐', 10)
words.add(name, 12)
# 随意插入新的词语
for i in range(1000):
words.add('节日', nu