<reponame>Bruce979527682/DataAnalysis
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os

from wordcloud import WordCloud, STOPWORDS

deskpath = os.path.join(os.path.expanduser("~"), 'Desktop')
currentpath = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(currentpath, 'alice.txt'), "rt", encoding="utf-8").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(currentpath, "alice_mask.png")))

stopwords = set(STOPWORDS)
stopwords.add("said")
font = r'simhei.ttf'
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue', font_path=font)

# generate word cloud
wc.generate(text)

# store to file
wc.to_file(path.join(deskpath, "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()
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os

from wordcloud import WordCloud, STOPWORDS

deskpath = os.path.join(os.path.expanduser("~"), 'Desktop')
currentpath = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(currentpath, 'alice.txt'), "rt", encoding="utf-8").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(currentpath, "alice_mask.png")))

stopwords = set(STOPWORDS)
stopwords.add("said")
font = r'simhei.ttf'
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue', font_path=font)

# generate word cloud
wc.generate(text)

# store to file
wc.to_file(path.join(deskpath, "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()
# Read the whole text. # read the mask image # taken from # http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg # generate word cloud # store to file # show