Python生成词云图wordcloud

1 环境

  • jieba
  • wordcloud
  • PIL
  • numpy
  • matplotlib
  • python3.5

2 例证

  • Demo
import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
# font = '/home/xdq/.local/share/fonts/SpaceMono-Regular.ttf'
font = '/usr/share/fonts/truetype/arphic/ukai.ttc'
def get_wordcloud(txt_dir, img_dir):
    with open(txt_dir, 'r',encoding='utf8') as f:
        content = f.read()
        print(content)
        cut = jieba.cut(content)
        print(cut)
        cut_content = ' '.join(cut)
        print(cut_content)
        # PIL打开图片
        img = Image.open(img_dir)
        # 获取图片矩阵信息
        img_array = np.array(img)
        # 设置目标图像属性:背景色,图层,字体,字体数量,尺寸
        wc = WordCloud(background_color='white', mask=img_array, font_path=font, max_words=1000, max_font_size=100, width=1800, height=1800)
        # 提取文字,生成云图内容
        img = wc.generate_from_text(cut_content)
        # 显示图片
        plt.imshow(img)
        # 保存图片
        wc.to_file('new.png')
# 文档文件        
txt_dir = "fullstack.txt"
# 原始图片(提取图片轮廓)
img_dir = "./images/7.jpeg"
get_wordcloud(txt_dir, img_dir)
  • Result
    在这里插入图片描述
图2.1 词云图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值