This article mainly introduces you to relevant information on how to use Python to create your own personalized logo in ten minutes. It mainly uses word clouds to achieve this effect. The article introduces it in great detail through example code, which is very useful for everyone. Studying or working has certain reference and learning value. Friends who need it can take a look below.
Preface
I believe everyone can use the word cloud. It is no longer unfamiliar. It is very simple to use. Just call the wordcloud package directly. Its main function is to generate pictures based on text words and word frequency, from which you can intuitively see the proportion of each word.
Recently I just wanted to make a personal logo, so I decided to use a word cloud to create it.
wordcloud installation
pip install wordcloud
You will definitely encounter a pitfall when installing using pip. An error may be reported during the installation process, prompting you to install Microsoft Visual C 14.0, but this installation process is time-consuming.
There is another way to solve it, which is to download the corresponding whl file and install it.
After the file is downloaded, cmd into 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
After successful installation, we will start making our pictures immediately.
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__) # 读文本文件 text = open(path.join(d, 'data.txt')).read() # 读取自定义图片 alice_coloring = np.array(Image.open(path.join(d, "pic.jpg"))) # 你可以通过 mask 参数 来设置词云形状 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('开始加载文本') # 改变字体颜色 img_colors = ImageColorGenerator(alice_coloring) # 字体颜色为背景图片的颜色 wc.recolor(color_func=img_colors) # 显示词云图 plt.imshow(wc, interpolation="bilinear") # 是否显示x轴、y轴下标 plt.axis('off') plt.show() # 获得模块所在的路径的 d = path.dirname(__file__) # 将多个路径组合后返回 wc.to_file(path.join(d, "h16.jpg")) print('生成词云成功!')
The above text content is collected through a crawler. It crawls an article about data science. After you have the text source, just prepare the image you want to generate a word cloud.
Configuring the parameters of wordcloud is particularly important for image effects. Let’s focus on the meaning of the parameters of wordcloud:
font_path: string font path, whatever font needs to be displayed Write the suffix name of the font path, such as: font_path = 'Heibody.ttf'
width: int (default=400) The output canvas width, the default is 400 pixels
height : int (default=200) The output canvas height, the default is 200 pixels
prefer_horizontal : float (default=0.90) Horizontal typesetting of words appears Frequency, default 0.9
#mask : nd-array or None (default=None) If the parameter is empty, the word cloud will be drawn normally. If mask is non-empty, the set width and height values will be ignored and the shape will be replaced by mask. The parts except white will not be drawn and the rest will be used to draw the word cloud.
scale : float (default=1) Enlarge the canvas according to the proportion. If set to 2, the length and width will be twice the original canvas.
min_font_size : int (default=4) The minimum font size displayed
font_step : int (default=1) The font step size, If the step size is greater than 1, it will speed up the operation but may lead to larger errors in the results.
max_words : number (default=200) The maximum number of words to be displayed
stopwords : set of strings or None Setting needs to be blocked word, if it is empty, the built-in STOPWORDS
background_color: color value (default=”black”) background color, such as background_color=’white’, the background color is white.
max_font_size : int or None (default=None) The maximum font size displayed
mode : string (default=”RGB”) When the parameter is "RGBA" and background_color is not empty, the background is transparent.
relative_scaling : float (default=.5) Correlation between word frequency and font size
color_func : callable, default=None Generate new colors The function, if empty, uses self.color_func
regexp : string or None (optional) Use regular expressions to separate the input text
collocations : bool, default=True Whether to include the collocation of two words
colormap : string or matplotlib colormap, default=”viridis” Randomly assign a color to each word, if specified color_func, this method is ignored.
Effect display
The photos used are the blogger’s own photos.
——————end——————
Related recommendations:
Use python socket to send http(s) request method
The above is the detailed content of Use Python to create your own personalized logo in ten minutes. For more information, please follow other related articles on the PHP Chinese website!