-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathword_cloud.py
42 lines (34 loc) · 1.12 KB
/
word_cloud.py
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
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 04 00:25:10 2017
@author: Yoga Lin
"""
from wordcloud import WordCloud, ImageColorGenerator
from scipy.misc import imread
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
words = open('cloud.txt')
word_count = []
for line in words.readlines():
word = line.strip().split(':')
word_count.append((unicode(word[0]), int(word[1])))
bg_mask = np.array(Image.open('timg.png'))
wc = WordCloud( font_path='./font/msyh.ttc',#设置字体
background_color="white", #背景颜色
max_words=2000,# 词云显示的最大词数
mask = bg_mask,
max_font_size=90, #字体最大值
random_state=41,
scale = 3
)
wc.fit_words(word_count)
image_colors = ImageColorGenerator(bg_mask)
plt.figure()
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis('off')
plt.show()
wc.to_file('test.png')