Python实现根据指定图片生成词云

编程入门 行业动态 更新时间:2024-10-14 00:29:15

Python实现根据指定<a href=https://www.elefans.com/category/jswz/34/1770705.html style=图片生成词云"/>

Python实现根据指定图片生成词云

效果

生成词云前

生成词云后

实现

新建imageWordCloud.py

在同级目录下新建aobama.txt,里面内容是英文单词(奥巴马演讲稿)

在同级目录下放一张照片bg.jpg,根据这张照片来生成词云

代码带注释:

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import osfrom wordcloud import WordCloud, STOPWORDS# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
# 获取目录,path.dirname(__file__):获取当前文件的path
# os.getcwd()方法用于返回当前工作目录。
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()# Read the whole text.
# 读取文件aobama.txt
text = open(path.join(d, 'aobama.txt')).read()# read the mask image
# taken from
# /255fk.jpg
#读取照片
alice_mask = np.array(Image.open(path.join(d, "bg.jpg")))
#设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
stopwords = set(STOPWORDS)
stopwords.add("said")
#生成词云,里面是具体参数
#max_words=2000 要显示的词的最大个数
#background_color="white" 背景颜色
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,stopwords=stopwords, contour_width=3, contour_color='steelblue')# generate word cloud
#生成词云
wc.generate(text)# store to file
# 存为照片文件
wc.to_file(path.join(d, "alice.png"))# show
#显示照片文件
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis("off")
plt.show()

 

更多推荐

Python实现根据指定图片生成词云

本文发布于:2024-03-05 20:10:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图片   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!