【问题标题】:How to put texts in image via WordCloud in Jupyter Notebook如何在 Jupyter Notebook 中通过 WordCloud 将文本放入图像中
【发布时间】:2020-08-23 11:30:49
【问题描述】:

我在 Jupyter Notebook 中显示通过 WordCloud 放入图像的文本时遇到问题。

下面是我的图片。

图形的输出基于矩形中的文本未显示在图像中定义的文本中的结果。

这是输出

我该如何解决?

这是我下面定义的代码 sn-p。

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

【问题讨论】:

  • 我不明白你想做什么。也许会显示预期的结果。
  • @furas 结果有问题。输出基于矩形中的单词而不是文本中的单词。
  • 我不明白你的意思是什么“矩形”。我在代码中看不到任何变量rectangle。也许显示有问题的图像与你得到的结果并显示你期望的图像。
  • @furas 我编辑了我的帖子。
  • 如果您只想在NETFLIX 中放置文本,那么您必须在白色背景上创建带有黑色NETFLIX 的图像。请参阅 wordcloud 文档中的图片:masked - 白底黑字

标签: python matplotlib word-cloud


【解决方案1】:

如果您想将结果放在单词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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 2020-10-18
    • 2017-06-07
    • 2020-07-20
    • 2012-08-04
    • 2020-01-15
    • 2013-07-22
    相关资源
    最近更新 更多