python词云自定义形状_用python生成自定义形状的wordcould(词云)

#!/usr/bin/env python

"""Image-colored wordcloud=======================You can color a word-cloud by using an image-based coloring strategyimplemented in ImageColorGenerator. It uses the average color of the regionoccupied by the word in a source image. You can combine this with masking -pure-white will be interpreted as 'don't occupy' by the WordCloud object whenpassed as mask.If you want white as a legal color, you can just pass a different image to"mask", but make sure the image shapes line up."""

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__")

# Read the whole text.

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

# read the mask / color image taken from

# http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010

# amazon_coloring = np.array(Image.open(path.join(d, "Picture1.png")))

# stopwords = set(STOPWORDS)

# stopwords.add("said")

icon = Image.open(path.join(d, "book6.jpeg")).convert("RGBA")

# change the size of canvas

mask = Image.new("RGB", icon.size, (1200,1200,1200))

mask.paste(icon,icon)

amazon_coloring = np.array(mask)

stopwords = set(STOPWORDS)

stopwords.add("said")

wc = WordCloud(background_color="white", max_words=2000, mask=amazon_coloring,

stopwords=stopwords, max_font_size=40, random_state=42)

# generate word cloud

wc.generate(text)

# create coloring from image

image_colors = ImageColorGenerator(amazon_coloring)

# show

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

plt.axis("off")

plt.figure()

# recolor wordcloud and show

# we could also give color_func=image_colors directly in the constructor

plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")

plt.axis("off")

plt.figure()

plt.imshow(amazon_coloring, cmap=plt.cm.gray, interpolation="bilinear")

plt.axis("off")

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值