在Jupyter Notebook中,我有一个关于显示通过WordCloud放入图像的文本的问题。
下面是我的图像。
图的输出是基于在图像中定义的文本中不显示矩形中的文本的结果。
下面是输出结果
如何解决这个问题?
这是我的代码片段,定义如下。
plt.figure(figsize=[15,15])
char_mask = np.array(Image.open("images/netflix.png"))
image_colors = ImageColorGenerator(char_mask)
wordcloud = WordCloud(stopwords=STOPWORDS,background_color = 'white',
width = 1000,
height = 1000,
max_words =300,
mask=char_mask).generate(' '.join(netflix_df['title']))
wordcloud.recolor(color_func=image_colors)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most Popular Words in Title',fontsize = 30)
plt.show()
如果你想把结果放在word里面 NETFLIX
然后制造黑色 NETFLIX
在白色背景上。
在任何照片编辑器中都可以轻松完成(如 GIMP 或 Photoshop
)
顺便说一下:我加了 contour_width=
和 contour_color=
查看是否有代码 NETFLIX
形象上)
形象。
结果:
代码: 代码。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from PIL import Image, ImageOps
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
netflix_df = pd.DataFrame({'title': ['King Kong', 'Rambo', 'Rambo II', 'Rambo III', 'James Bond',]})
text = ' '.join(netflix_df['title']
char_mask = np.array(Image.open("netflix.png")) # black NETFLIX on white background
wordcloud = WordCloud(stopwords=STOPWORDS,
background_color='white',
#width=1000,
#height=1000,
max_words=300,
mask=char_mask,
contour_width=3,
contour_color='steelblue',
).generate(text))
#image_colors = ImageColorGenerator(np.array(image))
#wordcloud.recolor(color_func=image_colors)
plt.figure(figsize=[15, 15])
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most Popular Words in Title', fontsize=30)
plt.show()
文档。屏蔽的wordcloud
与您的原始图像(红色 NETFLIX
在黑色的bacground上)你可以尝试转换为灰度并反转。
from PIL import Image, ImageOps
image = Image.open("netflix.jpg") # red NETFLIX on white background
image_gray = image.convert('L')
image_invert = ImageOps.invert(image_gray)
#image_invert.show() # it shows result
char_mask = np.array(image_invert)
#im = Image.fromarray(char_mask)
#im.show()
但原 jpg
图像的红色有许多不同的值,遮罩也不完美。
它需要更多的工作 - 即在范围内过滤颜色。
char_mask[ char_mask < 200 ] = 0
char_mask[ char_mask > 200 ] = 255
结果是
代码:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from PIL import Image, ImageOps
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
netflix_df = pd.DataFrame({'title': ['King Kong', 'Rambo', 'Rambo II', 'Rambo III', 'James Bond',]})
text = ' '.join(netflix_df['title']
image = Image.open("netflix.jpg") # red NETFLIX on black background
image_gray = image.convert('L')
image_invert = ImageOps.invert(image_gray)
#image_invert.show()
char_mask = np.array(image_invert)
char_mask[ char_mask < 200 ] = 0
char_mask[ char_mask > 200 ] = 255
#print(char_mask)
#im = Image.fromarray(char_mask)
#im.show()
wordcloud = WordCloud(stopwords=STOPWORDS,
background_color='white',
#width=1000,
#height=1000,
max_words=300,
mask=char_mask,
contour_width=3,
contour_color='steelblue',
).generate(text))
image_colors = ImageColorGenerator(np.array(image))
wordcloud.recolor(color_func=image_colors)
plt.figure(figsize=[15,15])
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most Popular Words in Title',fontsize = 30)
plt.show()
另外: 如果你创建白色 NETFLIX
黑底
那你就可以得到
代码。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from PIL import Image, ImageOps
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
netflix_df = pd.DataFrame({'title': ['King Kong', 'Rambo', 'Rambo II', 'Rambo III', 'James Bond',]})
text = ' '.join(netflix_df['title']
image = Image.open("netflix.jpg") # red NETFLIX on black background
image_gray = image.convert('L')
#image_gray.show()
char_mask = np.array(image_gray)
char_mask[ char_mask < 50 ] = 0
char_mask[ char_mask > 50 ] = 255
print(char_mask)
#im = Image.fromarray(char_mask)
#im.show()
wordcloud = WordCloud(stopwords=STOPWORDS,
background_color='white',
#width=1000,
#height=1000,
max_words=300,
mask=char_mask,
contour_width=3,
contour_color='steelblue',
).generate(text))
image_colors = ImageColorGenerator(np.array(image))
wordcloud.recolor(color_func=image_colors)
plt.figure(figsize=[15,15])
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most Popular Words in Title',fontsize = 30)
plt.show()
BTW: 要反转灰度图像,你也可以使用
char_mask = ~char_mask # invert gray scale