amueller / word_cloud

A little word cloud generator in Python
https://amueller.github.io/word_cloud
MIT License
10.13k stars 2.32k forks source link

Is there a way to put also a background color inside the shape? #632

Open Orosenthal opened 3 years ago

amueller commented 3 years ago

Hi. Can you please explain a bit more what you are looking for? You can use full masks, but the background color is usually the same everywhere, with the color coming from the wordcloud. It should be relatively straight-forward to change the color outside the mask in post-processing, though.

Orosenthal commented 3 years ago

Hi Andreas, Yes, of course. For example, take the Twitter logo and fill it with common words. The thing is that I also want to change the background inside the logo itself (on which the words will appear) so that eventually a bird will remain on a white outer background with the words on a light blue background inside. Thanks. (BTW love your youtube class lectures)

Orosenthal commented 3 years ago

my_twitter_wordcloud_2_small111

amueller commented 3 years ago

And "background_color" doesn't work?

amueller commented 3 years ago

Or is the issue that then you'll also get the color outside the mask?

Orosenthal commented 3 years ago

Or is the issue that then you'll also get the color outside the mask?

Exactly :)

amueller commented 3 years ago

Ok, but then you can just use your original mask to change the background color on the outside, right?

Orosenthal commented 3 years ago

Ok, but then you can just use your original mask to change the background color on the outside, right?

twitter_mask = imageio.imread('./twitter_mask.png') wordcloud = WordCloud(width=800, height=500, stopwords = he_s_words, font_path = 'C:\Windows\Fonts\courbd.ttf',random_state=10, background_color='white',mask=twitter_mask,max_words=50,contour_color='blue', colormap='ocean',collocations=False, contour_width=0.15, min_font_size= 10).generate(get_display(text)) **** I only have the option to change the 'background_color' which changes all the figure (inside and outside)

this is the twitter_mask.png' twitter_mask

amueller commented 3 years ago

well you can do background_color='blue' and then with the word cloud image that you get as result, you can

wc_image[twitter_mask == 255] = 255

or something like that to set the things outside the mask to white.

Orosenthal commented 3 years ago

Thanks:)

St3g1 commented 3 years ago

Hi guys. I'd be interested in the same feature but I seem to understand slower than oro. I think I got this word cloud image you mentioned via the to_ image method. I have no idea how to manipulate the individual pixels of this object though. Could you please provide a little more insight into how I can work this out, too? Thanks Stegi

amueller commented 3 years ago

Hey Stegi. The line I gave above manipulates the pixels via broadcasting. It sets all the pixels that are outside the mask (where the mask is white) to 255 for all three channels, resulting in white.