import jieba
import numpy
from PIL import Image#導入PIL模塊處理圖片
from wordcloud import WordCloud #導入詞雲模塊
words = open('F:\python\studyClasses\work\poet.txt','r').read()#打開歌詞文件,獲取到歌詞
new_words = ' '.join(jieba.cut(words))#使用jieba.cut分詞,然后把分好的詞變成一個字符串,每個詞用空格隔開
mask = numpy.array(Image.open(r'F:\python\studyClasses\work\tree.jpg'))
#使用pil模塊打開這個圖片,然后用numpy獲取到這個圖片各種屬性
wordcloud = WordCloud(width=1000, #圖片的寬度
height=860, #高度
margin=2, #邊距
mask=mask,#背景圖片
background_color='#d4ff80',#指定背景顏色
font_path='./simsun.ttc',#指定字體文件
scale=4 # 數值調節清晰度,越大越清晰
)
wordcloud.generate(new_words) #分詞
wordcloud.to_file('treepoet4.jpg')#保存到圖片