Skip to content

Instantly share code, notes, and snippets.

@jshannon75
Created November 28, 2019 15:14
Show Gist options
  • Save jshannon75/b0a0d11d95f771216d8276a44d3a5fa6 to your computer and use it in GitHub Desktop.
Save jshannon75/b0a0d11d95f771216d8276a44d3a5fa6 to your computer and use it in GitHub Desktop.
from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os
from wordcloud import WordCloud, STOPWORDS
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
# Read the whole text.
text = open(path.join(d, 'lols_text.txt')).read()
# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
us_mask = np.array(Image.open(path.join(d, "usa_shape1.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="white", max_words=2000, mask=us_mask,
stopwords=stopwords, contour_width=3, contour_color='steelblue')
# generate word cloud
wc.generate(text)
# store to file
wc.to_file(path.join(d, "words.png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment