详细讲解一下怎么用python的三方库wordcloud制作一个关于歌曲《Vincent》的歌词,有特别背景的云词效果,如图所示:

首先的先准备好一张背景图,为了云词效果,可以实现修改一下,为了方便识别:

然后就是歌曲《Vincent》的txt文档,最好将他们放在一起(可以不用写路径),

如果需要输入路径,一定不能有中文!!会无法识别的

然后就直接上代码:

 # -*- coding: utf-8 -*-
# @Time : 2018/7/27 18:20
# @Author : wjh
# @File : dpcq_yunci.py
with open('Vincent.txt', 'w', encoding='utf-8') as f:
f.write('''vincent—Starry night Starry starry night
paint your palette blue and grey
look out on a summer's day
with eyes that know the darkness in my soul Shadows on the hills
sketch the trees and the daffodils
catch the breeze and the winter chills
in colors on the snowy linen land And now I understand
what you tried to say to me
and how you suffered for your sanity
and how you tried to set them free
They would not listen
they did not know how
perhaps they'll listen now ''') import os
from wordcloud import WordCloud
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np # 把图片变成矩阵的形式
mask = np.array(
Image.open(
os.path.join(
os.path.dirname(__file__),'景甜-(背景纯).png'))) # 注意修改自己图片的名字 text = open('Vincent.txt', encoding='utf-8').read()
font = 'D:\Python\wordcloud\Hiragino Sans GB.ttc' # 路径不能有中文! wd = WordCloud(font_path=font, # 解决显示口字型乱码问题,
# 可进入D:\Python\wordcloud\Hiragino Sans GB.ttc
# max_font_size=50, #最大字体设置
# max_words=400, # 最大词数
width=800, # 画布宽度
height=400, # 画布高度
margin=1, # 字体之间宽度
mask=mask, # 以该参数值作图绘制词云,
# 这个参数存在时,width和height会被忽略
background_color='black', # 背景颜色
random_state=42, #设置有多少种随机生成状态,即有多少种配色方案
)
wd1 = wd.generate(text) # 将图片以“ceshi.png”文件保存
wd1.to_file('test.png')
# 开始绘制云图
plt.figure()
# 操作:加图片小标题
plt.title('Vincent 云词') # 中文会乱码
# 操作:去掉x,y轴数值
plt.axis('off')
# 操作:显示图片
plt.imshow(wd)
# 展示给用户
plt.show()

编辑器里面显示如图:

代码中关于font的路径文件,是为了显示中文文字,比如用赵雷的《彩虹下面》,用的python标志做的云词图片:

字体包:

最新文章

  1. Excel单元格发生变化后,使用Outlook给特定的人发邮件
  2. C#的变迁史 - C# 3.0篇
  3. 【BZOJ】1854: [Scoi2010]游戏
  4. bzoj4044
  5. Electron(一)--初步了解并动手HelloWorld
  6. 编写javascript的基本技巧
  7. ServiceStack 入门(二)
  8. 关于jquery选择器中:first和:first-child和:first-of-type的区别及:nth-child()和:nth-of-type()的区别
  9. 为什么我的Android SDK Manager中只显示已安装的package?
  10. rowcommand事件中获取控件
  11. Kafka 0.8源码分析—ZookeeperConsumerConnector
  12. Java基础笔记11
  13. PyQt中对RadioButton分组
  14. JAVA应用程序转换为Applet
  15. 干货——详解Java中的关键字
  16. Spark注册UDF函数,用于DataFrame DSL or SQL
  17. 将mysql数据库数据以Excel文件的形式导出
  18. 线程安全,有状态,无状态的对象<转>
  19. Linux原始套接字实现分析---转
  20. shell编程——内部变量

热门文章

  1. poj3469 Dual Core CPU——最小割
  2. codevs3162抄书问题(划分型dp)
  3. [Swift通天遁地]四、网络和线程-(3)线程组:使用DispatchGroup(调度组)对线程进行分组管理
  4. [Swift通天遁地]七、数据与安全-(12)使用Instruments Leaks工具检测内存泄露
  5. JavaSE综合项目演练
  6. curl怎么模拟登录进行采集
  7. sql数据库中常用连接
  8. 【洛谷3546_BZOJ2803】[POI2012]PRE-Prefixuffix(String Hash)
  9. Jquery音频播放插件下载地址(有Html、JS、CSS、音频)
  10. Xml的读取