使用python 3

时间:2017-08-22 02:42:40

标签: python word-cloud

def create_wordcloud(sd):
    mask = np.array(Image.open('line.jpg'))
    image_color = wordcloud.ImageColorGenerator(mask)
    word_cloud = wordcloud.WordCloud(background_color='white',
                                     width=1000,
                                     height=1000,
                                     margin=2,
                                     mask=mask,
                                  font_path='simhei.ttf').fit_words(sd)
    plt.imshow(word_cloud.recolor(color_func=image_color))
    plt.axis('off')
    plt.show()

    word_cloud.to_file('test1.png')

我使用python 3创建了一个词云,参考了Github官方文档中的例子。

一开始我的代码工作得很好,但问题是当我使用PhotoShop将我的掩蔽图片('line.jpg')从彩色图片改为草图样式时,代码引发的错误如下:

  

font_size = int(2 * sizes [0] * sizes [1] /(sizes [0] + sizes [1]))

     

IndexError:列表索引超出范围

我看了一下源代码并意识到我可以通过指定'max_font_size'参数来避免这个错误。我这样做只是为了获得无效的结果图片。

我愿意知道如何解决这个问题。

PS: 图2是原始图片的一部分,图片是正在重塑的图片。

enter image description here enter image description here

0 个答案:

没有答案