需求:
看到朋友圈有人发词云照片,感觉自己也可以玩一玩,于是乎借助wordcloud实现功能。
环境:
MacOS 10.12 +Python 2.7 +Wordcloud
Windows通用
准备:
安装wordcloud
$ pip install wordcloud
SIP功能是Apple在OSX上推出的系统完整性保护功能,新版本的macOS直接用pip安装报错,在不关闭SIP功能的前提下,可以使用
$ pip install wordcloud --user -U
某些情况还会提示错误,需要安装VS for Python,直接上官网下载安装即可。
实现:
源码
#! /usr/bin/env python # import from os import path from PIL import Image import numpy as np import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS # current path d = path.dirname(__file__) # Read the whole text. text = open(path.join(d, 'test.txt')).read() # read the mask image test_mask = np.array(Image.open(path.join(d, "test_mask.png"))) stopwords = set(STOPWORDS) stopwords.add("said") # setting wc = WordCloud(background_color="black", max_words=2000, mask=test_mask, stopwords=stopwords) # generate word cloud wc.generate(text) # plot and show plt.imshow(wc, interpolation='bilinear') plt.axis("off") plt.figure() plt.imshow(test_mask, cmap=plt.cm.gray, interpolation='bilinear') plt.axis("off") plt.show() # store to file wc.to_file(path.join(d, "test.png"))
辅助文件
test_mask.png
test.txt
The list of big's prior run-ins with the far-right fringe is long and varied. He tweeted fake crime statistics spread by racists to paint black cans as violent, then defended them as credible. He sparked a firestorm last year when he declined to renounce
效果图
其它说明:
1.文档可以是任意英文txt文件,以上的是网络上英文新闻中的一小段,仅起示例作用。
2.关于中文支持,有多种方法,主要就是分词的问题,这里不讨论了。
3.我不清楚许多人说的定制是什么意思,因为如果想要自己任意想要的形状的话,我觉得用PS做一个png图可以达到同样的效果,经测试也没有发现问题。
4.写的第一篇文章,不足之处欢迎来喷,毕竟我是要学习的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
Python,词云
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《张韶涵 全面沦陷》[WAV/分轨][370MB]
- 《极品汽车音乐 三十而立 一起走过的岁月 2CD》[WAV/分轨][990MB]
- 周冰倩1997-珍藏精选-真的好想你[WAV+CUE]
- 许廷铿2017《THEULTIMATECOLLECTION》2CD[星梦娱乐][WAV+CUE]
- 徐小明1983-再向虎山行(LP版)[文志][WAV+CUE]
- 《黑神话:悟空》石双双COS热潮持续升温 丝袜你喜欢么
- Qiddiya IRL游戏之旅在东京电玩展重磅登场
- 北京首家任天堂Switch体验店开业:国行机立减200元
- 张秀卿.1999-赢【神采】【WAV+CUE】
- 群星.1977-多多宝丽多(LP版)【宝丽金】【WAV+CUE】
- 刘德华.1989-永远...记得你(2012金碟复刻版)【EMI百代】【WAV+CUE】
- 《维纳斯璀璨假期死或生Xtreme》游戏特色玩法
- 《完蛋我被美女包围了前传》角色介绍
- 2024年免费游戏加速器推荐,稳定才是王道!
- 《蛋仔派对》小海鹦兰兰皮肤图鉴