Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. # coding: utf-8
  2. #========================================
  3. # author: Changlong.Zang
  4. # mail: zclongpop123@163.com
  5. # time: Tue Sep 24 14:54:13 2019
  6. #========================================
  7. import os
  8. import numpy
  9. import jieba
  10. from PIL import Image
  11. from wordcloud import WordCloud, color_from_image
  12. #--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  13. def main(text_path, input_image_path, output_image_path):
  14. '''
  15. '''
  16. if not os.path.isfile(text_path):
  17. return
  18.  
  19. if not os.path.isfile(input_image_path):
  20. return
  21.  
  22. #-
  23. txt_data = ''
  24. with open(text_path) as f:
  25. txt_data = f.read()
  26.  
  27. count_data = dict()
  28. for w in jieba.cut(txt_data, cut_all=False):
  29. if len(w) < 2:
  30. continue
  31. count_data[w] = count_data.get(w, 0) + 1
  32.  
  33. image_mask = numpy.array(Image.open(input_image_path))
  34. _wc = WordCloud(font_path='C:/Windows/Fonts/simhei.ttf', mask=image_mask, background_color='white')
  35. _wc.generate_from_frequencies(count_data)
  36. _wc.recolor(color_func=color_from_image.ImageColorGenerator(image_mask))
  37. _wc.to_file(output_image_path)
  38.  
  39.  
  40. if __name__ == '__main__':
  41. main('D:/aaa.txt', 'D:/aaa.jpg', 'D:/bbb.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement