In [6]:
# %load /home/andy/checkout/word_cloud/examples/a_new_hope.py
#!/usr/bin/env python2
"""
Using custom colors
====================
Using the recolor method and custom coloring functions.
"""

from os import path
from scipy.misc import imread
import matplotlib.pyplot as plt
import random



def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)

d = "/home/andy/checkout/word_cloud/examples/"


mask = imread(path.join(d, "stormtrooper_mask.png"))

text = open(path.join(d, "a_new_hope.txt")).read()

# preprocessing the text a little bit
text = text.replace("HAN", "Han")
text = text.replace("LUKE'S", "Luke")

text = text.replace("INT", "")
text = text.replace("EXT", "")

from wordcloud import WordCloud

wc = WordCloud(mask=mask).generate(text)
wc.recolor(color_func=grey_color_func)

plt.imshow(wc)

In [ ]: