python绘制人物肖像(词云)

准备工作

  1. 安装wordcloud库(pip install wordcloud)网速不好是可以使用国内镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wordcloud
  1. 安装中文分词需要的jieba库(pip install jieba)或使用国内镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple jieba

  1. 找一份制作词云使用的文字(txt格式)
  2. 制作所需要的背景图
  3. 搭建好环境
背景图

要求:背景需要色差较大的图片,若是不满足,可使用修图工具自行修改
photoshop修改图片阈值
在这里插入图片描述

制作词云
1、英文版
# 导入wordcloud模块和matplotlib模块
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import imageio
# imageio.imread()

# 读取一个txt文件
text = open('file/The Count of Monte Cristo.txt','r',encoding="utf8").read()
# 读入背景图片
bg_pic = imageio.imread('file/201906170848597.jpg')
# 生成词云
wordcloud = WordCloud(mask=bg_pic,background_color='white',max_words=3000,max_font_size=80,min_font_size=5).generate(text)
# 保存词云图片
wordcloud.to_file("file/out_pic.jpg")
plt.imshow(wordcloud)
plt.axis('off')
plt.show()

2、中文版
# 导入wordcloud模块和matplotlib模块

from wordcloud import WordCloud
import jieba
import imageio
from os import path
import matplotlib.pyplot as plt

# 绘制词云
def draw_wordcloud():
    # 读入一个txt文件
    # comment_text = open('file/古诗词名句.txt','rb').read()
    comment_text = open('file/默读.txt','rb').read()
    # 结巴分词,生成字符串,如果不通过分词,无法直接生成正确的中文词云
    cut_text = " ".join(jieba.cut(comment_text))
    d = path.dirname(__file__) # 当前文件文件夹所在目录
    color_mask = imageio.imread("file/1514902082476.jpg") # 读取背景图片
    cloud = WordCloud(
        # 设置字体,不指定就会出现乱码
        font_path="方正喵呜体.ttf",
        # 设置背景色
        background_color='white',
        # 词云形状
        mask=color_mask,
        #允许最大词汇
        max_words=3000,
        #最大号字体
        max_font_size=150,
        #有多少种情况
        random_state=30

    )
    word_cloud = cloud.generate(cut_text) # 产生词云

    word_cloud.to_file("file/默读.jpg") # 保存图片
    #  显示词云图片
    plt.imshow(word_cloud)
    plt.axis('off')
    plt.show()

if __name__ == '__main__':
    draw_wordcloud()

在这里插入图片描述
这种情况是字体设置的问题,但是不影响结果
Building prefix dict from the default dictionary ... Loading model from cache C:\Users\柠檬の夏\AppData\Local\Temp\jieba.cache Loading model cost 3.215 seconds. Prefix dict has been built successfully.

3、word cloud其他参数设置
font_path : string //字体路径,需要展现什么字体就把该字体路径+后缀名写上,如:font_path = '黑体.ttf'
width : int (default=400) //输出的画布宽度,默认为400像素
height : int (default=200) //输出的画布高度,默认为200像素
prefer_horizontal : float (default=0.90) //词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 )
mask : nd-array or None (default=None) //如果参数为空,则使用二维遮罩绘制词云。如果 mask 非空,设置的宽高值将被忽略,遮罩形状被 mask 取代。除全白(#FFFFFF)的部分将不会绘制,其余部分会用于绘制词云。如:bg_pic = imread('读取一张图片.png'),背景图片的画布一定要设置为白色(#FFFFFF),然后显示的形状为不是白色的其他颜色。可以用ps工具将自己要显示的形状复制到一个纯白色的画布上再保存,就ok了。
scale : float (default=1) //按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
min_font_size : int (default=4) //显示的最小的字体大小
font_step : int (default=1) //字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差。
max_words : number (default=200) //要显示的词的最大个数
stopwords : set of strings or None //设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
background_color : color value (default=”black”) //背景颜色,如background_color='white',背景颜色为白色。
max_font_size : int or None (default=None) //显示的最大的字体大小
mode : string (default=”RGB”) //当参数为“RGBA”并且background_color不为空时,背景为透明。
relative_scaling : float (default=.5) //词频和字体大小的关联性
color_func : callable, default=None //生成新颜色的函数,如果为空,则使用 self.color_func
regexp : string or None (optional) //使用正则表达式分隔输入的文本
collocations : bool, default=True //是否包括两个词的搭配
colormap : string or matplotlib colormap, default=”viridis” //给每个单词随机分配颜色,若指定color_func,则忽略该方法。

fit_words(frequencies) //根据词频生成词云
generate(text) //根据文本生成词云
generate_from_frequencies(frequencies[, ...]) //根据词频生成词云
generate_from_text(text) //根据文本生成词云
process_text(text) //将长文本分词并去除屏蔽词(此处指英语,中文分词还是需要自己用别的库先行实现,使用上面的 fit_words(frequencies) )
recolor([random_state, color_func, colormap]) //对现有输出重新着色。重新上色会比重新生成整个词云快很多。
to_array() //转化为 numpy array
to_file(filename) //输出到文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柠檬の夏

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值