利用python?制作詞云特效詳情
什么是 ?詞云?
?詞云? 其實就是就是對網(wǎng)絡文本中出現(xiàn)頻率較高的〝關鍵詞〞予以視覺上的突出,形成〝關鍵詞云層〞或〝關鍵詞渲染〞從而過濾掉大量的文本信息
?詞云? 也是數(shù)據(jù)可視化的一種形式。給出一段文本,根據(jù)關鍵詞的出現(xiàn)頻率而生成的一幅圖像,人們只要掃一眼就能夠明白其文章主旨。
一、特效預覽
詞云圖:
二、程序原理
- 從給出的文本中,進行分詞處理,然后將每個詞出現(xiàn)的的頻率進行統(tǒng)計
- 從給出的背景圖片上,讀出圖片信息
- 將文本按照出現(xiàn)的頻率進行畫圖,出現(xiàn)頻率越高,字體設置越大
你學廢了嘛
三、程序源碼
- jieba模塊:用來進行分詞處理
- PIL模塊:用來進行圖片處理
- wordcloud模塊:用來進行生成詞云
#!/usr/bin/env python # encoding: utf-8 import jieba import numpy as np import PIL.Image as Image from wordcloud import WordCloud class wordCloud: ''' This is a main Class, the file contains all documents. One document contains paragraphs that have several sentences It loads the original file and converts the original file to new content Then the new content will be saved by this class ''' def __init__(self): self.bg_img = 'assets/picture.jpeg' self.word_path = 'assets/word.txt' def hello(self): ''' This is a welcome speech :return: self ''' print('*' * 50) print(' ' * 20 + '詞云制作') print(' ' * 5 + 'Author: autofelix Date: 2022-01-17 13:14') print('*' * 50) return self def run(self): ''' The program entry ''' with open(self.word_path, 'r') as f: word = f.read() cut_word = ' '.join(jieba.cut(word)) color_mask = np.array(Image.open(self.bg_img)) word_cloud = WordCloud( # 設置字體,不指定就會出現(xiàn)亂碼 font_path='/System/Library/Fonts/PingFang.ttc', # 設置背景色 background_color='white', # 詞云形狀 mask=color_mask, # 允許最大詞匯 max_words=120, # 最大號字體 max_font_size=2000 ).generate(cut_word) word_cloud.to_file('word_cloud.jpg') im = word_cloud.to_image() im.show() if __name__ == '__main__': wordCloud().hello().run()
到此這篇關于利用python 制作詞云特效詳情的文章就介紹到這了,更多相關python 制作詞云特效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺析python中5個帶key的內(nèi)置函數(shù)
這篇文章主要介紹了python中5個帶key的內(nèi)置函數(shù),包括max取最大值函數(shù),min取最小值函數(shù),filter過濾函數(shù),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07Python數(shù)據(jù)分析之?Matplotlib?3D圖詳情
本文主要介紹了Python數(shù)據(jù)分析之Matplotlib 3D圖詳情,Matplotlib提供了mpl_toolkits.mplot3d工具包來進行3D圖表的繪制,下文總結了更多相關資料,需要的小伙伴可以參考一下2022-05-05APPium+Python編寫真機移動端自動化腳本的項目實踐
本文主要介紹了APPium+Python編寫真機移動端自動化腳本的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07如何基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測
最近開始學習opencv,想檢測圖片上的圓環(huán),發(fā)現(xiàn)霍夫變換可以做這樣的效果出來,于是嘗試用霍夫變換做了下圓環(huán)檢測,這篇文章主要給大家介紹了基于OpenCV&Python實現(xiàn)霍夫變換圓形檢測的相關資料,需要的朋友可以參考下2021-08-08