We can create a word cloud image based on a white color background. If you want to use an image as a backgroud to create a word cloud image. How to do?
In this tutorial, we will introduce you how to create a word cloud image based on a background image.
Preliminary
We should import some libraries.
from wordcloud import WordCloud from PIL import Image import numpy as np
Read background image
We can use python pillow to read an image.
im = Image.open("bg.png") bg_pic = np.asarray(im)
Create word cloud image based on image
wc = WordCloud(mask=bg_pic, background_color='white', width = 300, height=300, margin=2) text = ''' from wordcloud import WordCloud from PIL import Image import numpy as np im = Image.open("bg.png") bg_pic = np.asarray(im) ''' wc.generate(text) wc.to_file('wc1.png')
In this example, we use backgroud image as a mask to create a word cloud image.
Run this result, you will get this word cloud image.