簡體   English   中英

我的 wordcloud 蒙版生成了一系列點,概述了蒙版應該在哪里,但文字適合整個圖像的形狀

[英]My wordcloud mask is producing a series of points outlining where the mask should be but the words are fitting to the shape of the entire image

如上所述,我的 wordcloud 沒有按照我以前見過的方式運行,我不知道是什么導致了這個問題,因為我以前做過,但從未遇到過這個問題。

# import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

這就是我的數據的樣子

data = pd.read_csv('cleanTwitterData.csv')
data


    Date    Close   name    tweet
3   2022-04-25  51.700001   elonmusk    hope that even worst critics remain Twitter be...
4   2022-04-26  49.680000   elonmusk    esaagar Suspending the Twitter account major n...
5   2022-04-27  48.639999   elonmusk    For Twitter deserve public trust must politica...
6   2022-04-28  49.110001   elonmusk    Let make Twitter maximum fun
7   2022-04-29  49.020000   elonmusk    The people Twitter strongly agree that Twitter...
... ... ... ... ...
176 2022-10-15  50.450001   elonmusk    KimDotcom Twitter trying hardest escalate this...
186 2022-10-25  52.779999   elonmusk    SwipeWright Twitter should broadly inclusive p...
187 2022-10-26  53.349998   elonmusk    Entering Twitter let that sink D68z4K2wq7
188 2022-10-27  53.700001   elonmusk    Dear Twitter Advertisers GMwHmInPAS
189 2022-10-28  53.700001   elonmusk    Comedy now legal Twitter

這是我用於遮罩的圖像

pil_im = Image.open('twitterLogo.png')
display(pil_im)

在此處輸入圖像描述

我的面具

mask = np.array(Image.open("twitterLogo.png"))
mask

array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [238, 238, 238],
        [238, 238, 238],
        [239, 239, 239]],

       [[255, 255, 255],
        [254, 254, 254],
        [254, 254, 254],
        ...,
        [237, 237, 237],
        [237, 237, 237],
        [238, 238, 238]],
...,
[254, 254, 254],
        [254, 254, 254],
        [255, 255, 255]],

       [[239, 239, 239],
        [238, 238, 238],
        [238, 238, 238],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)

所以在這一點上我認為它看起來不錯,面具看起來應該,數據看起來應該所以下一步是創建詞雲:

#Generate a word cloud image
text = " ".join(i for i in data.tweet)
stopwords = set(STOPWORDS)
wordcloud = WordCloud(stopwords=stopwords,background_color='white', max_words=1000, mask=mask,contour_color='#023075',contour_width=3,colormap='Blues').generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

但我得到的不是漂亮的 twitter 形狀的雲,而是一個矩形,其中 twitter 徽標的輪廓由小藍點固定(有點難以看到,但如果你仔細觀察,你可以辨認出 twitter 徽標的形狀:

在此處輸入圖像描述

我嘗試使用一個或兩個其他隨機 png 文件作為蒙版,結果相同。

有人可以指出我哪里出了問題嗎?

任何幫助將不勝感激。

正如 Paul Brodersen 所評論的那樣,用於蒙版的圖像必須是黑白的,黑色對應於要填充的區域。

謝謝保羅

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM