如果您想将结果放在单词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()
文档: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