Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- #========================================
- # author: Changlong.Zang
- # mail: zclongpop123@163.com
- # time: Tue Sep 24 14:54:13 2019
- #========================================
- import os
- import numpy
- import jieba
- from PIL import Image
- from wordcloud import WordCloud, color_from_image
- #--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
- def main(text_path, input_image_path, output_image_path):
- '''
- '''
- if not os.path.isfile(text_path):
- return
- if not os.path.isfile(input_image_path):
- return
- #-
- txt_data = ''
- with open(text_path) as f:
- txt_data = f.read()
- count_data = dict()
- for w in jieba.cut(txt_data, cut_all=False):
- if len(w) < 2:
- continue
- count_data[w] = count_data.get(w, 0) + 1
- image_mask = numpy.array(Image.open(input_image_path))
- _wc = WordCloud(font_path='C:/Windows/Fonts/simhei.ttf', mask=image_mask, background_color='white')
- _wc.generate_from_frequencies(count_data)
- _wc.recolor(color_func=color_from_image.ImageColorGenerator(image_mask))
- _wc.to_file(output_image_path)
- if __name__ == '__main__':
- main('D:/aaa.txt', 'D:/aaa.jpg', 'D:/bbb.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement