Skip to content

Instantly share code, notes, and snippets.

@tiaplagata
Last active January 28, 2021 14:51
Show Gist options
  • Save tiaplagata/7d009231839aa514b01eb33576235138 to your computer and use it in GitHub Desktop.
Save tiaplagata/7d009231839aa514b01eb33576235138 to your computer and use it in GitHub Desktop.
Use mask colors to recolor wordcloud
from wordcloud import ImageColorGenerator
#Create the mask
colosseum_mask = np.array(Image.open('/Users/tiaplagata/Downloads/colosseum.jpg'))
#Grab the mask colors
colors = ImageColorGenerator(colosseum_mask)
#Instantiate the wordcloud using color_func argument
cloud = WordCloud(mask=colosseum_mask,
background_color='white',
color_func=colors).generate_from_text(data)
#Plot the wordcloud
plt.figure(figsize=(10,8))
plt.imshow(cloud)
plt.axis('off')
plt.title(title)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment