Skip to content

Instantly share code, notes, and snippets.

@data-henrik
Last active October 5, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save data-henrik/876793ef104a3a992851c0aaee8ada68 to your computer and use it in GitHub Desktop.
Save data-henrik/876793ef104a3a992851c0aaee8ada68 to your computer and use it in GitHub Desktop.
Python script to generate wordcloud for given background image
# Masked wordcloud
# ================
# Using a mask you can generate wordclouds in arbitrary shapes.
import sys, getopt
from os import path
from PIL import Image
import numpy as np
import random
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
# opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
# print 'test.py -i <inputfile> -o <outputfile>'
if len(sys.argv) < 3:
print (sys.argv[1], " inputtext outputimage")
exit(2)
print len(sys.argv)
infile=sys.argv[1]
o_image=sys.argv[2]
masked=False
if len(sys.argv) == 4:
masked=True
maskimage=sys.argv[3]
d = path.dirname('.')
# Read the whole text.
text = open(path.join(d, infile)).read()
if masked==True:
# read the mask image
print "masked"
bm_mask = np.array(Image.open(path.join(d, maskimage)))
wc = WordCloud(max_words=2000, mask=bm_mask,
stopwords=STOPWORDS.add("said"))
else:
wc = WordCloud(max_words=2000,
stopwords=STOPWORDS.add("said"))
# generate word cloud
wc.generate(text)
# store default colored image
default_colors = wc.to_array()
plt.title("Custom colors")
plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3))
wc.to_file(o_image)
plt.axis("off")
plt.figure()
plt.title("Default colors")
plt.imshow(default_colors)
plt.axis("off")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment