Demo:
Not wordy, just go to the code first.
from wordcloud import WordCloud
import jieba
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
path = r'E:\123\教程' #文本路径
path_ph = r'E:\123\教程' #词云背景模板路径
font = r'C:\Windows\Fonts\FZSTK.TTF' #设置字体,可以显示中文。
text = (open(path + r'\job.txt', 'r', encoding='utf-8')).read() # gbk <--> utf-8
cut = jieba.cut(text) # 使用 jieba库 分词
string = ' '.join(cut)
print(len(string)) # 输出词量
img = Image.open(path_ph + r'\2.jpg') # 打开图片
img_array = np.array(img) # 将图片装换为数组
stopword = ['Unword'] # 设置停止词,也就是你不想显示的词,可有可无,看情况处理
wc = WordCloud(
background_color='white', #设置显示内容在什么颜色内
width=1000, #设置图片宽
height=800, #设置图片高
mask=img_array, #设置词云背景模板
font_path=font, #设置字体路径
stopwords=stopword,
scale=10 #图像清晰度,数值越大越清晰,最好在10-30之间。
)
wc.generate_from_text(string) # 绘制图片
plt.imshow(wc)
plt.axis('off') #关闭坐标轴
plt.show() # 显示图片
wc.to_file(path + r'\wordcloud1.png') # 保存图片
Before use, you need to download and install the required third-party libraries. (Wordcloud, jieba, matplotlib, PIL, numpy)
can be installed via pip.
If you still can't, you can read another article of mine-> pip install, update, uninstall, check module method
Note: Sometimes the word cloud you generated may not be clear, because the scale value is not set, and the generated word cloud image will become clear after setting.