链接: https://github.com/amueller/word_cloud
自定义词云:http://ywordle.ymark.cc/
文字符号:ww.txt
利用的照片:ww_heart.png
生成的照片:www.png
yum install -y python34-devel
from os import path
import jieba
from scipy.misc import imread
from wordcloud import WordCloud,STOPWORDS, ImageColorGenerator
import matplotlib.pyplot as plt
d = path.dirname('.')
texts = open(path.join(d, 'ww.txt')).read()
#你保存的文件里面的数据自己可以定义,数量决定了你生成图片里面内容的大小
alice_coloring = imread(path.join(d, "ww_heart.png"))
#一个心型的图片
def word_segment(texts):
jieba.analy.set_stop_words("ww.txt")
for text in texts:
tags=jieba.analyse.extract_tags(text,topK=20)
yield " ".join(tags)
def generate_img(texts):
#data=" ".join(text for text in texts)
mask_img=imread("ww_heart.png")
wc =WordCloud(
font_path='msyh.ttc',
background_color='white',
mask=mask_img
).generate(texts)
image_colors = ImageColorGenerator(alice_coloring)
plt.imshow(wc)
plt.axis('off')
plt.figure()
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis("off")
plt.figure()
plt.imshow(alice_coloring, cmap=plt.cm.gray)
plt.axis("off")
plt.show()
wc.to_file(path.join(d, "www.png"))
word_segment(texts)
generate_img(texts)
中间会报错: ImportError: No module named Tkinter
发现还是用python2比较好。