zoukankan      html  css  js  c++  java
  • 人形词云,根据图片黑白形状绘制词云

      人形词云,根据图片黑白形状,绘制词云:

    #coding:utf-8
    """
    Masked wordcloud
    ================
    
    Using a mask you can generate wordclouds in arbitrary shapes.
    """
    
    from os import path
    from PIL import Image
    import numpy as np
    import matplotlib.pyplot as plt
    
    from wordcloud import WordCloud, STOPWORDS
    
    d = path.dirname(__file__)
    
    # Read the whole text.
    text = open(path.join(d, '3.txt'),"rb").read()  #rb二进制读取,
    text=text.decode("utf-8") #按照utf-8解码
    #print text #utf-8
    
    # read the mask image
    # taken from
    # http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
    alice_mask = np.array(Image.open(path.join(d, "3.jpg")))
    
    stopwords = set(STOPWORDS)
    stopwords.add("said")
    
    wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
                   stopwords=stopwords,font_path="simkai.ttf")
    # 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()

  • 相关阅读:
    ApplicationContext.xml修改
    springmvc.xml约束
    log4j.properties
    SqlMapConfig.xml配置文件
    Mybatis注解式开发坐标
    字符串函数
    vim基础快捷键
    format的使用
    lambda匿名函数
    字典的基础使用
  • 原文地址:https://www.cnblogs.com/my-global/p/12447479.html
Copyright © 2011-2022 走看看