简单单词云制作程序的Python实现


浏览:544 次

要安装的软件包:

tkinter pick wordcloud pylab matplotlib jieba PIL numpy os imageo

(ps:如果未安装软件包,请使用pip install package_name安装软件包。)

最重要的功能:WorldCloud功能

wc=WordCloud(background_color=“white”,random_state=2,scale=1,mask=alice_coloring,stopwords=stopwords,collocations=False,font_path=myfont,width=1400,height=1400,margin=2)

WorldCloud功能的简要说明:

1.背景_颜色更改背景颜色,仅测试白色和黑色,其他未验证

2.使用比例改变单词云图像的精细度(值越大,越精细,越耗时;>8可能需要几分钟(测试集为10w个单词)

3.使用font_ Path=myfont更改单词云的字体(中文单词云必须依赖于用户定义的字体库)

4.最小字体大小最小字体大小(默认值为4)

5.轮廓_宽度字云轮廓的描述默认为0

6.文章末尾解释了WorldCloud功能

词云生成核心代码:

定义字云(d,str1,str2):

alice_着色=np。array(Image.open(path.coin(d,str2))#背景图像

stopwords=集合(stopwords)

stopwords.add(“说”)

image_colors=图像颜色生成器(alice_coloring)

myfont=(r'tyj.ttf')

wc=WordCloud(background_color=“white”,random_state=2,scale=1,mask=alice_coloring,stopwords=stopwords,collocations=False,font_path=myfont,width=1400,height=1400,margin=2)

txt=打开(路径.join(d,str1),编码=“utf-8”).read()

txt_杰巴=杰巴。cut(txt,cut_all=真)

txt_finish=“”.join(txt_jieba)

wc.生成(txt_finish)

plt.imshow(alice_coloring)

图片展示(wc)

plt.轴(“关闭”)

plt.show()

厕所。to_文件(路径.join(“name.jpg”))

wordcloud(d,txt,图片

完整代码:

导入tkinter.messagebox

进口泡菜

从wordcloud导入wordcloud、STOPWORDS、ImageColorGenerator

从pylab导入matplotlib,mpl

来自matplotlib。font_ manager导入FontManager

导入matplotlib。pyplot为plt

导入matplotlib

进口捷巴

从PIL导入图像

将numpy导入为np

从os导入路径

从imageio导入*

窗口=tk.tk()

窗title(“WordCloud演示app”)

窗几何('400x300')#这里的乘法是一个小x

canvas=厚度。画布(窗口,宽度=400,高度=135,背景=“绿色”)

image_file=tk.PhotoImage(file='pic.gif')

image=画布。create_image(200,0,anchor='n',image=image_file)

canvas.pack(side='top')

tk。标签(window,text='Welcome',font=('Arial',16)).pack()

tk。标签(窗口,text='text_Input',font=('Arial',14))。位置(x=10,y=170)

tk。标签(window,text='Picture_Input',font=('Arial',14))。位置(x=10,y=210)

var_ txt=tk.StringVar()

var_ txt.set('1.txt')

entry_ txt=tk。条目(window,textvariable=var_txt,font=('Arial',14))

entry_txt.place(x=130,y=175)

var_图片=tk.StringVar()

var_图片集('love.png')

entry_图片=tk。条目(window,textvariable=var_picture,font=('Arial',14))

entry_图片.place(x=130,y=215)

d=路径。dirname(__file__)if“__file__”在locals()else操作系统中。getcwd()#不理解,但从官方文档中复制

定义提交():

#这两行代码用于获取用户输入的txt和图片

txt=var_ txt.get()

picture=var_ picture.get()

打印(txt,图片)

定义字云(d,str1,str2):

alice_ coloring=np。array(Image.open(path.join(d,str2))#背景图像

stopwords=集合(stopwords)

stopwords.add(“说”)

image_colors=图像颜色生成器(alice_coloring)

myfont=(r'tyj.ttf')

wc=WordCloud(b

相关资讯:
农产品电子商务向企业个人农产品电子贸易方向发展的效益

农产品电子商务向企业个人农产品电子贸易方向发展的效益

查看
Web 3.0技术的基本方向

Web 3.0技术的基本方向

查看
认知

认知

查看
面向对象的

面向对象的

查看
2018-12-25

2018-12-25

查看