[英]wordcloud words not fitting to mask
I'm attempting to create a wordcloud from a dataframe, where there is a column of words and a column for their respective frequencies.我正在尝试从 dataframe 创建一个词云,其中有一列单词和一列用于它们各自的频率。 I've also downloaded an image that I want to use as a mask, for example: cloud image我还下载了一张我想用作蒙版的图像,例如:云图像
I can't get the wordcloud to fit to the mask - despite following the examples shown.我无法让 wordcloud 适合面具 - 尽管遵循所示示例。 Here is my code:这是我的代码:
from PIL import Image
import pandas as pd
import numpy as np
from wordcloud import WordCloud
import matplotlib.pyplot as plt
df = pd.DataFrame({'word':['time', 'car', 'Peter\'s', 'sky',
'something', 'computer', 'brain', 'plants',
'trees', 'Alien\'s', 'gun', 'eat'],
'freq':[24, 16, 16, 15,
14, 13, 12, 10,
9, 9, 5, 3]})
words = ' '.join(df['word'])
cloud_image = r"C:\Users\L\Documents\Python Scripts\cloud.png"
mask = np.array(Image.open(cloud_image))
wordcloud = WordCloud(max_words=2000,
width=1280,
height=720,
mask=mask)
wordcloud.generate_from_frequencies()
fig, ax = plt.subplots(figsize=(8, 6))
ax.imshow(wordcloud, interpolation='bilinear')
ax.axis("off")
ax.imshow(mask, interpolation='bilinear')
plt.show()
I know this question is old but I just spent the past hour trying to get an particular image to work as a mask.我知道这个问题很老,但我刚刚花了一个小时试图让特定图像用作蒙版。 The primary issue I was facing was that the image I wanted to use as my mask for my wordcloud did not have a white (RGB 255,255,255) background.我面临的主要问题是,我想用作 wordcloud 蒙版的图像没有白色 (RGB 255,255,255) 背景。 The solution I ended-up with:我最终得到的解决方案:
The result is your original image with a white (RGB 255,255,255) background.结果是带有白色 (RGB 255,255,255) 背景的原始图像。 wordcloud's mask will now treat your image as you expected! wordcloud 的蒙版现在将按照您的预期处理您的图像!
As it turns out, it was just a problem with the size/type of the image.事实证明,这只是图像大小/类型的问题。 It worked with a different mask image (although I had previously tried two images before without success, hence the post).它使用不同的蒙版图像(尽管我之前曾尝试过两张图像但没有成功,因此这篇文章)。
Some images should be adjusted for the process.一些图像应该针对这个过程进行调整。 Note only white point values for image is mask_out (other values are mask_in).请注意,只有图像的白点值是 mask_out(其他值是 mask_in)。 The problem is that some of images are not suitable for masking.问题是有些图像不适合屏蔽。 The reason is that the color's np.array somewhat mismatches.原因是颜色的 np.array 有点不匹配。 To solve this, following can be done: 1.Creating mask object: (Please try with your own image as I couldn't upload:)要解决此问题,可以执行以下操作: 1.创建掩码 object:(请尝试使用您自己的图像,因为我无法上传:)
import numpy as np;
import pandas as pd;
from PIL import Image;
from wordcloud import WordCloud
mask = np.array(Image.open("filepath/picture.png"))
print(mask)
If the output values for white np.array is 255, then it is okay.如果 white np.array 的 output 值为 255,那么就可以了。 But if it is 0 or probably other value, we have to change this to 255.但如果它是 0 或者可能是其他值,我们必须将其更改为 255。
2.In the case of other values, the code for changing the values: 2-1. 2.如果是其他值,改值代码:2-1。 Create function for transforming (here our value = 0)创建 function 用于转换(这里我们的值 = 0)
def transform_zeros(val):
if val == 0:
return 255
else:
return val
2-2. 2-2。 Creating the same shaped np.array:创建相同形状的 np.array:
maskable_image = np.ndarray((mask.shape[0],mask.shape[1]), np.int32)
2-3. 2-3。 Transformation:转型:
for i in range(len(mask)):
maskable_image[i] = list(map(transform_zeros, mask[i]))
3.Checking: 3.检查:
print(maskable_image)
Then you can use this array for your mask.然后你可以使用这个数组作为你的面具。
mask = maskable_image
All this is copied and interpreted from this link , so check it if you find my attempted explanation unclear, as I just provided solution but don't understand that much about color arrays of image and its transformation.所有这些都是从这个链接复制和解释的,所以如果你发现我尝试的解释不清楚,请检查它,因为我只是提供了解决方案但对图像的颜色 arrays 及其转换不太了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.