pythonnumpymatplotlibimshowword-cloud

wordcloud with 2 background colors


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.


Solution

  • 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].