大数据 爬虫Python AI SqlPython学习

Python十分钟制作属于你自己的个性logo

2018-11-22  本文已影响2人  1a076099f916

词云的使用相信大家已经不陌生了,使用很简单,直接调用wordcloud包就可以了。它的主要功能是根据文本词汇和词汇频率生成图片,从中可以直观的看出各个词汇所占比重。最近正好想做一个人的logo,于是乎决定使用词云来制作完成。

wordcloud安装

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install wordcloud
</pre>

使用 pip 安装你肯定会遇到一个坑,安装过程中可能会报错,提示你安装 Microsoft Visual C++ 14.0,但是这个安装过程很耗时。

有另一个方法可以解决,就是下载相应的whl文件安装。博主下载的是 wordcloud-1.4.1-cp36-cp36m-win_amd64.whl 文件,如果下载不了,可以在公众号后台输入 “wordcloud” 获取。

下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud

文件下载后,cmd 进入whl文件所在文件夹下,然后输入以下命令:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install wordcloud-1.4.1-cp36-cp36m-win_amd64.whl
</pre>

Python十分钟制作属于你自己的个性logo

wordcloud代码使用

安装成功后我们马上开始制作我们的图片,代码如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
d = path.dirname(file)

读文本文件

text = open(path.join(d, 'data.txt')).read()

读取自定义图片

alice_coloring = np.array(Image.open(path.join(d, "pic.jpg")))

你可以通过 mask 参数 来设置词云形状

wc = WordCloud(background_color="white",max_words=2000,
mask=alice_coloring, max_font_size=60,random_state=102,scale=8,
font_path="C:WindowsFontsmsyhbd.ttf").generate(text)
wc.generate_from_text(text)
print('加载文本')

改变字体颜色

img_colors = ImageColorGenerator(alice_coloring)

字体颜色为背景图片的颜色

wc.recolor(color_func=img_colors)

显示词云图

plt.imshow(wc, interpolation="bilinear")

是否显示x轴、y轴下标

plt.axis('off')
plt.show()

获得模块所在的路径的

d = path.dirname(file)

将多个路径组合后返回

wc.to_file(path.join(d, "h16.jpg"))
print('生成词云成功!')
</pre>

上面 text 文本内容是通过爬虫采集的,爬取的关于数据科学的一篇文章,有了文本源之后准备好你想要生成词云的图片就行了。

配置 wordcloud 的参数对于图片效果尤为重要,下面我们着重介绍一下wordcloud的参数含义:

效果展示

Python十分钟制作属于你自己的个性logo

点击关注,加小编Python学习群:813542856就可获得Python学习资料

Python十分钟制作属于你自己的个性logo
上一篇 下一篇

猜你喜欢

热点阅读