微慑信息网

python下wordcloud词云

 

测试例子:

 

#coding:utf-8
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
import numpy
from PIL import Image
import io
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


def doWordCloud():
	txt = io.open("test.txt",'r',encoding='GBK').read()
	image = numpy.array(Image.open('./test.jpg'))
	wc = WordCloud(
	    background_color="black",
	    font_path="C:/Windows/Fonts/STKAITI.TTF",
	    max_font_size=60,
	    max_words=3000,
	    mask = image,
	).generate(txt)
	plt.figure(figsize=(10,10))
	#重定向颜色
	image_color = ImageColorGenerator(image)
	plt.imshow(wc.recolor(color_func=image_color))
	plt.axis('off')
	plt.show()
	#保存图片
	wc.to_file("./result.jpg")

def start():
	doWordCloud()
def main():
	start()

if __name__=="__main__":
	main()

测试数据:https://new.qq.com/omn/20210302/20210302A0DZHY00.html

测试结果:

拓展阅读(点评/知识):

https://www.cnblogs.com/jlutiger/p/9176517.html

https://blog.csdn.net/u010309756/article/details/67637930

https://www.jb51.net/article/183811.htm

本文标题:python下wordcloud词云
本文链接:
(转载请附上本文链接)
http://vulsee.com/archives/vulsee_2021/0303_13930.html
转载请附本站链接,未经允许不得转载,,谢谢:微慑信息网-VulSee.com » python下wordcloud词云
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

微慑信息网 专注工匠精神

访问我们联系我们