3

I generated this on wordcloud.com using one of the "themes".

enter image description here

I'd like to be able to do this with the python wordcloud library, but so far all I can achieve is a single background color (so all black, not grey and black). Can anyone give me a hint on how to add the additional background color, using matlab or imshow? Here is my code:

import numpy as np 
import matplotlib.pyplot as plt
    
from pathlib import Path 
from PIL import Image 
from wordcloud import WordCloud
    
tmp = "some text about Dynasty TV show"
    
alexis_mask = np.array(Image.open('resources/alexis-poster-3.png')) 
print(repr(alexis_mask)) alexis_mask[alexis_mask == 0] = 255
    
def color_func(word, font_size, position, orientation, random_state=None,
    **kwargs):
        return "hsl(0, 100%, 27%)"
    
wc = WordCloud(background_color="black", mask=alexis_mask, max_words=500,contour_width=2, contour_color="black")
    
wc.generate(tmp) 
plt.figure(figsize=(28, 20)) plt.imshow(wc.recolor(color_func=color_func, random_state=3),interpolation="bilinear") 
plt.imshow(wc)

I've tried starting with a black/white image, and with a black/white/grey image. So far neither works. I don't think it's offered in the Wordcloud library but is it something I could do using imshow(), after I apply wordcloud? Thanks.

1 Answer 1

3

You can draw the wordcloud with the desired background color (e.g."grey") and then overlay this plot with a uniformly colored image (e.g. "black") masked using the wordcloud mask.

import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from PIL import Image

text = "some text about Dynasty TV show"

x, y = np.ogrid[:300, :300]
mask = (((x - 150)**2 + (y - 150)**2 > 130**2) * 255).astype(int)

wc = WordCloud(background_color="grey", repeat=True, mask=mask)
wc.generate(text)

plt.axis("off")
plt.imshow(wc)
plt.imshow(np.dstack([Image.new("RGB", (wc.width,wc.height), "black"), wc.mask]))
plt.show()

enter image description here

The minimal expample works for the case where the mask is 0 and 255 only. In the general case of a 1D mask it would be (wc.mask == 255) * 255, for a RGBA mask it would be ((wc.mask[:,:,0:3] == [255, 255, 255]) * 255)[:,:,0].

1
  • Excellent! I see how the 2 separate plt.imshow() calls did it, first referring to the wordcloud, then the mask that would be overlayed. Awesome! Here is my [result] (kaggle.com/code/heatherpropes/wordcloud-practice) on kaggle if you are interested.
    – nettie
    Commented Aug 20 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.