1. 程式人生 > >Python實現根據指定圖片生成詞雲

Python實現根據指定圖片生成詞雲

效果

生成詞雲前

生成詞雲後

實現

新建imageWordCloud.py

在同級目錄下新建aobama.txt,裡面內容是英文單詞(奧巴馬演講稿)

在同級目錄下放一張照片bg.jpg,根據這張照片來生成詞雲

程式碼帶註釋:

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os

from wordcloud import WordCloud, STOPWORDS

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
# 獲取目錄,path.dirname(__file__):獲取當前檔案的path
# os.getcwd()方法用於返回當前工作目錄。
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
# 讀取檔案aobama.txt
text = open(path.join(d, 'aobama.txt')).read()

# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
#讀取照片
alice_mask = np.array(Image.open(path.join(d, "bg.jpg")))
#設定需要遮蔽的詞,如果為空,則使用內建的STOPWORDS
stopwords = set(STOPWORDS)
stopwords.add("said")
#生成詞雲,裡面是具體引數
#max_words=2000 要顯示的詞的最大個數
#background_color="white" 背景顏色
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue')

# generate word cloud
#生成詞雲
wc.generate(text)

# store to file
# 存為照片檔案
wc.to_file(path.join(d, "alice.png"))

# show
#顯示照片檔案
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()