如何在 Jupyter Notebook 中通过 WordCloud 将文本放入图像中

How to put texts in image via WordCloud in Jupyter Notebook

我在 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()

如果您想将结果放在单词 NETFLIX 中,则在白色背景上创建黑色 NETFLIX

在任何照片编辑器(如 GIMPPhotoshop)中更容易做到这一点

(顺便说一句:我添加了 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()

文档:Masked wordcloud


使用您的原始图像(红色 NETFLIX 黑色背景),您可以尝试转换为灰度并反转它。

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()

顺便说一句: 反转灰度图像你也可以使用

char_mask = ~char_mask  # invert gray scale