posted on 2023-05-21 17:13 read(582) comment(0) like(7) collect(5)
Hello everyone, I am Kongkong star. In this article, I will share with you how to generate words through Python's wordcloud
词云(心形形状)
library .
The wordcloud library for Python is a Python package for generating word clouds . It can arrange the words with high frequency in a piece of text into a word cloud graph according to their frequency of occurrence and color depth, so as to better display the information in the text.
Using the wordcloud library, you can specify parameters such as the shape, color, and font of the word cloud, and control attributes such as the arrangement and size of words in the word cloud. You can use the wordcloud library to generate various types of word clouds, such as word frequency statistics, sentiment analysis, etc.
pip install wordcloud
pip show wordcloud
Name: wordcloud
Version: 1.8.2.2
Summary: A little word cloud generator
Home-page: https://github.com/amueller/word_cloud
Author: Andreas Mueller
Author-email: t3kcit+wordcloud@gmail.com
License: MIT
Requires: matplotlib, numpy, pillow
Required-by:
import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud
local = '/Users/kkstar/Downloads/video/pic/'
text = "大家好我是空空star我爱发动态我喜欢使用搜索引擎模式进行分词"
cut_text = " ".join(jieba.cut_for_search(text))
mask = np.array(Image.open(local+"heart.png"))
wc = WordCloud(font_path="STHeitiMedium.ttc", # 设置字体
background_color='white', # 设置背景颜色
mask=mask, # 设置背景图片
max_words=2000, # 设置最大显示的词数
max_font_size=100, # 设置字体最大值
width=500, # 设置画布的宽度
height=500, # 设置画布的高度
)
wc.generate(cut_text)
wc.to_file(local+"wordcloud.png")
To set the heart shape of WordCloud, you need to use the mask parameter to specify the path of the heart image, use Python's Pillow library to load the heart image, and pass it to the WordCloud object.
Introduction to jieba library and pillow library:
Segment text through Python's jieba library
Add text watermark to pictures through Python's PIL library
Author:evilangel
link:http://www.pythonblackhole.com/blog/article/25298/9489f76b1050b5ac3258/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!