接上文生成詞云1
采用不同的底圖机打,效果也不一樣
好用的png圖片網(wǎng)站
(https://pngimg.com/image)
關(guān)鍵在設(shè)置詞云圖的參數(shù)mask與img,設(shè)定img為'D:/cat.png'且mask設(shè)為img残邀。
詞云效果如下
"""制作詞云圖"""
# 讀取圖像
img = imageio.imread('D:/cat.png')
# img = Image.open('D:/cat.png')
# 設(shè)置詞云圖
wc = wordcloud.WordCloud(
# width=1000, # 詞云圖的寬
# height=700, # 圖片的高
background_color= 'black', # 詞云圖背景顏色
font_path='msyh.ttc', # 詞云字體, 微軟雅黑, 系統(tǒng)自帶
scale=10, # 字體大小
mask=img,
stopwords=set([line.strip() for line in open('cn_stopwords.txt', mode='r',
encoding='utf-8').readlines()])
)
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 構(gòu)建詞云對(duì)象
wordcloud = WordCloud(width=800, height=800, background_color='white', font_path='msyh.ttc',
min_font_size=10, max_words=200)
# 讀入文本文件
with open('G:/lyric/test/仗劍.txt', 'r') as f:
text = f.read()
# 生成詞云圖像
wordcloud.generate(text)
# 顯示詞云圖像
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
# 保存詞云圖像
wordcloud.to_file("G:/d.png")
# 導(dǎo)入必要的庫(kù)
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import imageio # 圖像模塊
img = imageio.imread('D:/cat.png')
# 構(gòu)建詞云對(duì)象
wordcloud = WordCloud(width=800, height=800, background_color='white', font_path='msyh.ttc',mask=img,
min_font_size=10, max_words=200)
# 讀入文本文件
with open('G:/lyric/test/仗劍.txt', 'r') as f:
text = f.read()
# 生成詞云圖像
wordcloud.generate(text)
# 顯示詞云圖像
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
# 保存詞云圖像
wordcloud.to_file("G:/d2.png")