天天看點

python使用背景圖檔做詞雲圖

 首先準備資料和圖檔:

圖檔:backgroud_Image = plt.imread('0.jpg')#路徑

open("ciyuntu.txt" ,encoding="utf-8")as file:   #打開資料檔案

from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS
import jieba
import numpy
import PIL.Image as Image
import matplotlib.pyplot as plt
#1.将字元串切分
def chinese_jieba(text):
    wordlist_jieba=jieba.cut(text)
    space_wordlist=" ".join(wordlist_jieba)
    return space_wordlist
with open("ciyuntu.txt" ,encoding="utf-8")as file:
    text=file.read()
    text=chinese_jieba(text)
    #2.圖檔遮罩層
    # mask_pic=numpy.array(Image.open("dog.png"))

    backgroud_Image = plt.imread('0.jpg')
    #3.将參數mask設值為:mask_pic
    wordcloud = WordCloud(background_color='white',max_words=10000,font_path="C:/Windows/Fonts/simkai.ttf",stopwords=STOPWORDS,mask=backgroud_Image).generate(text)
    image_colors = ImageColorGenerator(backgroud_Image)
    image=wordcloud.to_image()
    image.show()
           

背景圖檔:

python使用背景圖檔做詞雲圖

效果:

python使用背景圖檔做詞雲圖