-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (58 loc) · 1.99 KB
/
main.py
File metadata and controls
70 lines (58 loc) · 1.99 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author : ellenbboe
import os
import jieba
from imageio import imread
from wordcloud import WordCloud
class Generator:
def __init__(self, words,stopwords, color, font, image=None):
super(Generator, self).__init__()
self.words = words
self.stopwords = stopwords
self.color = color
self.font = font
self.image = image
def generate(self):
with open(self.stopwords, encoding='utf-8') as f_stop:
f_stop_text = f_stop.read()
f_stop_seg_list = f_stop_text.splitlines()
# 读入文本内容
text = open(self.words, encoding='utf-8').read()
# 中文分词
seg_list = jieba.cut(text, cut_all=False)
# 把文本中的stopword剃掉
my_word_list = []
for my_word in seg_list:
if len(my_word.strip()) > 1 and not (my_word.strip() in f_stop_seg_list):
my_word_list.append(my_word)
my_word_str = ' '.join(my_word_list)
# 字体不要包含中文,否则会报错!
font_path = self.font
if self.image:
wc = WordCloud(
font_path=font_path,
background_color=self.color,
mask=imread(self.image),
)
else:
wc = WordCloud(
font_path=font_path,
background_color=self.color,
random_state=1024,
width=1920,
height=1080,
)
try:
wc.generate(my_word_str)
wc.to_file('images/output.png')
except Exception as e:
print(e)
if __name__ == '__main__':
path = os.getcwd()
image_generator = Generator(words="./input.txt",
stopwords="./stopwords.txt",
font="./Normal.otf",
color="black",
image="./shape.jpeg")
image_generator.generate()