python actual battle = = = old driver's skillful characters are converted into pictures

First, put two renderings:

And this:

Is it right~

Here is an interesting module called wordcloud:


Official website:


*It is recommended to install it by downloading pip install may not be successful.

Open it and download it:'cloud/archive/

Then python install

Install other dependent modules:

You must install the first step to install numpy:



Download the what file, and then PIP install xxxx.who

If there is an error, please refer to

Share the source code:

from os import path from scipy.misc import imread import matplotlib.pyplot as plt import jieba from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator stopwords = {} def importStopword(filename=''): global stopwords f = open(filename, 'r', encoding='utf-8') line = f.readline().rstrip() while line: stopwords.setdefault(line, 0) stopwords[line] = 1 line = f.readline().rstrip() f.close() def processChinese(text): seg_generator = jieba.cut(text) # Use jieba Participle, or not seg_list = [i for i in seg_generator if i not in stopwords] seg_list = [i for i in seg_list if i != u' '] seg_list = r' '.join(seg_list) return seg_list importStopword(filename='./stopwords.txt') # Get current file path # __file__ Is the current file, stay ide Running this line in will report an error,Can be changed to # d = path.dirname('.') d = path.dirname(__file__) text = open(path.join(d, u'love.txt'),encoding ='utf-8').read() #If it's Chinese text = processChinese(text)#Poor segmentation in Chinese, using Jieba Participle proceeding # read the mask / color image # Set background picture back_coloring = imread(path.join(d, "./image/love.jpg")) wc = WordCloud( font_path='./font/cabin-sketch.bold.ttf ', #Set font use Chinese characters simhei.ttf background_color="white", #background color max_words=1000,# Maximum number of words displayed in word cloud mask=back_coloring,#Set background picture max_font_size=80, #Font maximum random_state=10, #42 ) # Generative word cloud, It can be used generate Enter all text(Poor segmentation in Chinese),It can also be used after we have calculated the word frequency generate_from_frequencies function wc.generate(text) # wc.generate_from_frequencies(txt_freq) # txt_freq Examples are[('Word a', 100),('Word b', 90),('Word c', 80)] # Generate color values from background pictures image_colors = ImageColorGenerator(back_coloring) plt.figure() # The following code shows the picture plt.imshow(wc) plt.axis("off") # Draw word cloud # Save picture wc.to_file(path.join(d, "Name 1.png"))


The official samplecode gives an example of the rendering:

#!/usr/bin/env python
Image-colored wordcloud

You can color a word-cloud by using an image-based coloring strategy
implemented in ImageColorGenerator. It uses the average color of the region
occupied by the word in a source image. You can combine this with masking -
pure-white will be interpreted as 'don't occupy' by the WordCloud object when
passed as mask.
If you want white as a legal color, you can just pass a different image to
"mask", but make sure the image shapes line up.

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator

d = path.dirname(__file__)

# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()

# read the mask / color image taken from
alice_coloring = np.array(, "alice_color.png")))
stopwords = set(STOPWORDS)

wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
               stopwords=stopwords, max_font_size=40, random_state=42)
# generate word cloud

# create coloring from image
image_colors = ImageColorGenerator(alice_coloring)

# show
plt.imshow(wc, interpolation="bilinear")
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.imshow(alice_coloring,, interpolation="bilinear")

Finally, thanks to

Source code of he ta project:

Tags: Python github pip encoding

Posted on Sun, 03 May 2020 18:53:09 -0400 by persepha