1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| from random import randint
import jieba import wordcloud from imageio import imread
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None): h = randint(0, 60) s = int(100.0 * 255.0 / 255.0) l = int(50.0 * 255.0 / 255.0) return "hsl({}, {}%, {}%)".format(h, s, l)
mask = imread("党徽.png") f = open("讲话.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(mask=mask, font_path="PingFang.ttc", width=562, height=562, background_color="white", color_func=random_color_func, collocations=False, scale=8) w.generate(txt) w.to_file("讲话词云.png")
|