-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path411 track 1
65 lines (60 loc) · 2.13 KB
/
411 track 1
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
#coding=utf8
# note: reference words type from arial.ttf
# background picture retrieved from internet
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
#去除停用词,并统计词频
def word_freq():
fr = open("stopwords.txt", "r")
stopwords = []
for line in fr:
curline = line.strip("\n").split("\t")
stopwords.append(curline[0])
fr = open("data1.txt", "r")
word_freq_dict = {}
for line in fr:
curline = line.split(" ")
for w in curline:
if w in stopwords:
continue
if w not in word_freq_dict:
word_freq_dict[w] = 0
word_freq_dict[w] += 1
return word_freq_dict
#随机获取颜色
def get_color():
return (random.randint(1, 255), random.randint(1, 255), random.randint(1, 255))
#获取图片位置
def get_position(width, height):
width_rand = random.randint(0, width+40)
height_rand = random.randint(0, height+40)
return (width_rand, height_rand)
if __name__ == "__main__":
iList = 50
word_frequency = word_freq()
word_sort = sorted(word_frequency.items(), key=lambda d:d[1], reverse=True)[0:iList]
# for word, freq in word_sort:
# print (word, freq)
# print (len(word_sort))
if len(word_sort) <= 0:
raise ValueError("ERROR : the number of words needs to be more than ZERO.")
#统计所有词出现的总次数
freq_sum = 0
for word, freq in word_sort:
freq_sum += freq
#画图
im01 = Image.open('background.png')
for word, freq in word_sort:
#获取图片的宽、长
width, height = im01.size
#计算词在图片上展示的相对大小;手动调整290是个需要手动调整的参数
font_size = int((2 * freq / freq_sum) * 450)
draw = ImageDraw.Draw(im01)
#arial.ttf
font = ImageFont.truetype("Candara.ttf", font_size)
draw.text(get_position(width, height),word, fill = get_color(), font=font)
# draw.text(get_position(width, height),word, fill = get_color(), font=font)
#输出,保存在当前目录,文件是out.png,
im01.save("out.png")