This article mainly introduces you about 10 minutes how to use Python to make your own personal logo of the relevant information, the main use of the word cloud to achieve this effect, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, You need a friend, let's take a look.
Objective
The use of Word clouds believe that we are not unfamiliar with the use of simple, direct call Wordcloud package on it. Its main function is based on the text vocabulary and vocabulary frequency generation of images, which can be intuitively see the proportion of each vocabulary.
Recently just want to be a person's logo, and decided to use the word cloud to make the completion.
Wordcloud Installation
Pip Install Wordcloud
With PIP installation you will definitely encounter a pit that may error during installation, prompting you to install Microsoft Visual C + + 14.0, but this installation process is time-consuming.
There is another way to solve this is to download the appropriate WHL file installation.
After the file is downloaded, cmd enters the folder where the WHL file is located, and then enter the following command:
Pip Install WORDCLOUD-1.4.1-CP36-CP36M-WIN_AMD64.WHL
Wordcloud Code Usage
Once the installation is successful, we'll start making our pictures.
From OS import pathfrom PIL import imageimport numpy as Npimport matplotlib.pyplot as Pltfrom wordcloud import Wordcloud, Stopwords, Imagecolorgeneratord = Path.dirname (__file__) # Read Text file = open (Path.join (d, ' data.txt ')). Read () # Read custom Picture alice_coloring = Np.array (Image.open (Path.join (D, "pic.jpg")) # You can set the word cloud shape with the mask parameter WC = Wordcloud (background_ Color= "White", max_words=2000, mask=alice_coloring, max_font_size=60,random_state=102,scale=8, Font_path = "C:\Windows\Fonts\msyhbd.ttf"). Generate (text) wc.generate_from_text (text) print (' Start loading this ') # change font Color img_colors = Imagecolorgenerator (alice_coloring) # font color for background picture color Wc.recolor (color_func=img_colors) # display word cloud plt.imshow (WC, interpolation= "bilinear") # Whether to display x-axis, y-axis subscript plt.axis (' off ') plt.show () # Get the path where the module is located d = Path.dirname (__file__) # Combine multiple paths to return Wc.to_file (Path.join (D, "h16.jpg")) print (' Generate Word cloud success! ')
Above text content is collected by crawlers, crawling of data science of an article, with the text source ready you want to create a word cloud image on the line.
Configuring Wordcloud parameters is especially important for picture effects, so let's focus on the Wordcloud parameter meanings:
font_path:string font path, you need to show what font to put the font path + suffix name, such as: Font_path = ' blackbody. TTF '
Width:int (default=400) output canvas width, default is 400 pixels
Height:int (default=200) output canvas height, default to 200 pixels
Prefer_horizontal:float (default=0.90) The frequency of horizontal typesetting of words, default 0.9
Mask:nd-array or None (default=none) If the argument is empty, the word cloud is drawn normally. If mask is not empty, the set width height value is ignored and the shape is replaced by mask. The portions except White will not be drawn and the remainder will be used to draw the word cloud.
Scale:float (default=1) enlarges the canvas proportionally, as set to 2, which is twice times the length and width of the original canvas.
Min_font_size:int (default=4) The smallest font size displayed
Font_step:int (default=1) font step, if the step is greater than 1, will speed up the operation but may result in a large error.
Max_words:number (default=200) The maximum number of words to display
Stopwords:set of strings or None sets the words that need to be masked, and if empty, uses the built-in stopwords
Background_color:color value (default= "BLACK") background color, such as background_color= ' white ', and the background color of the black.
Max_font_size:int or None (default=none) displays the maximum font size
Mode:string (default= "RGB") when the parameter is "RGBA" and the Background_color is not empty, the background is transparent.
Relative_scaling:float (default=.5) The correlation between word frequency and font size
Color_func:callable, default=none a function that generates a new color, and if it is empty, uses Self.color_func
Regexp:string or None (optional) uses regular expressions to separate input text
Collocations:bool, whether the default=true includes two words to match
Colormap:string or Matplotlib ColorMap, default= "viridis" randomly assigns a color to each word, and if you specify Color_func, the method is ignored.
Effect Show
The photos used are bloggers ' own photos.
—————— End ——————