Use python to implement personalized word cloud, and use python to personalize word cloud
First slice
Word cloud
Template required
pip install jiebapip install wordcloud
I also need to install two other things. I don't know much about them either.
pip install scipypip install matplotlib
Because ubuntu is not as troublesome as windows, and there are not so many errors.
I can see that many people are excited about making their own word cloud. If I am excited, I will do it right away. Impulse is the first creativity.
Jieba is a very hateful Chinese Word Segmentation template.
Jeba Chinese Document
As for wordcloud, there are no Chinese documents, but we can guess it. If it is not good at English, we can guess it and check the source code.
Contribute all my code first
#-*-Coding: UTF-8 -- *-import jieba. analysefrom wordcloud import WordCloud, ImageColorGeneratorfrom scipy. misc import imreadimport matplotlib. pyplot as pltclass wc: def _ init _ (self, txt_file, img_file, font_file): self. f = open (txt_file, 'R') self.txt = self. f. read () self. f. close () self. tags = jieba.analyse.extract_tags(self.txt, topK = 100) # The topK returns several keywords self. text = ''. join (self. tags) # link the word segmentation, and add spaces because English relies on Space word segmentation self. img = imread (img_file) self. wc = WordCloud (font_path = font_file, background_color = 'white', max_words = 100, mask = self. img, max_font_size = 80) ### click here to guess #### font_path indicates the font file path, because the fonts provided by wordcloud do not support Chinese characters, We need to specify a font file, otherwise, all output images are framed. # background_color: Black by default. I set it to white. # max_words: Maximum number of words displayed. # mask background image # max_font_size: Maximum font size. self. word_cloud = self. wc. generate (self. text) def show_wc (self): # img_color = ImageColorGenerator (self. img) plt. imshow (self. word_cloud) # You can use plt. imshow (self. wc. recolor (color_func = img_color) to make the image color the same as the font color plt. axis ("off") plt. show () if _ name __= = '_ main _': mywc = wc('sanwen.txt', 'out.png ', 'font. ttc ') mywc. show_wc ()
Use wc. recolor (color_func = img_color)