Python如何制作圣诞树和圣诞树词云(python,开发技术)

时间:2024-04-28 00:31:54 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、前言

圣诞节庆祝和送礼物貌似现在已经成为全球流行的习惯~

Python如何制作圣诞树和圣诞树词云

本文利用 Python 制作圣诞树和词云,教会你多种方法,代码直接运行即可,学会拿去送给你想要祝福的人吧~~

二、Python画圣诞树

1. 圣诞树1号

#-*-coding:UTF-8-*-"""@Author:叶庭云@公众号:AI庭云君@CSDN:https://yetingyun.blog.csdn.net/"""importturtlescreen=turtle.Screen()screen.setup(800,600)circle=turtle.Turtle()circle.shape('circle')circle.color('red')circle.speed('fastest')circle.up()square=turtle.Turtle()square.shape('square')square.color('green')square.speed('fastest')square.up()circle.goto(0,280)circle.stamp()k,j=0,0foriinrange(1,17):y=30*iforjinrange(i-k):x=30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()ifi%4==0:x=30*(j+1)circle.color('red')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()k+=2ifi%4==3:x=30*(j+1)circle.color('yellow')circle.goto(-x,-y+280)circle.stamp()circle.goto(x,-y+280)circle.stamp()square.color('brown')foriinrange(17,20):y=30*iforjinrange(3):x=30*jsquare.goto(x,-y+280)square.stamp()square.goto(-x,-y+280)square.stamp()turtle.mainloop()

效果如下:

Python如何制作圣诞树和圣诞树词云

2. 圣诞树2号

#-*-coding:UTF-8-*-"""@Author:叶庭云@公众号:AI庭云君@CSDN:https://yetingyun.blog.csdn.net/"""fromturtleimport*importturtleimportrandomn=100.0speed(96)turtle.setup(width=800,height=720)#用screensize设置的是画布大小及背景色screensize(800,800,"White")left(90)forward(3*n)color("red","yellow")#五角星的颜色begin_fill()left(126)foriinrange(5):forward(n/5)right(144)forward(n/5)left(72)end_fill()right(126)#圣诞树颜色color("#00CC00")backward(n*4.8)deftree(d,s):ifd<=0:returnforward(s)tree(d-1,s*.8)right(120)tree(d-3,s*.5)right(120)tree(d-3,s*.5)right(120)backward(s)tree(15,n)backward(n/2)foriinrange(200):a=200-400*random.random()b=10-20*random.random()up()forward(b)left(90)forward(a)down()ifrandom.randint(0,1)==0:color('#FF0066')else:color('#FF6600')circle(2)up()backward(a)right(90)backward(b)turtle.mainloop()

效果如下:

Python如何制作圣诞树和圣诞树词云

还可以更改背景,加上下雪特效!如下所示:

#画雪花关键代码defdrawsnow():#定义画雪花的方法t.ht()#隐藏笔头,ht=hideturtlet.pensize(2)#定义笔头大小foriinrange(200):#画多少雪花t.pencolor("white")#定义画笔颜色为白色,其实就是雪花为白色t.pu()#提笔,pu=penupt.setx(r.randint(-350,350))#定义x坐标,随机从-350到350之间选择t.sety(r.randint(-100,350))#定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始t.pd()#落笔,pd=pendowndens=6#雪花瓣数设为6snowsize=r.randint(1,10)#定义雪花大小forjinrange(dens):#就是6,那就是画5次,也就是一个雪花五角星#t.forward(int(snowsize))#int()取整数t.fd(int(snowsize))t.backward(int(snowsize))#t.bd(int(snowsize))#注意没有bd=backward,但有fd=forward,小bugt.right(int(360/dens))#转动角度drawsnow()#调用画雪花的方法

Python如何制作圣诞树和圣诞树词云

3. 圣诞树3号

画一颗漂亮的圣诞树!!代码过长,限于篇幅不贴在文中,效果如下所示:

Python如何制作圣诞树和圣诞树词云

三、Python制作圣诞树词云

做词云得有关键词素材,这就去百度文库高校版下载一些圣诞祝福文章~~

Python如何制作圣诞树和圣诞树词云

12篇差不多够了吧

Python如何制作圣诞树和圣诞树词云

接下来上 Python 代码!!!!!!

#-*-coding:UTF-8-*-"""@Author:叶庭云@公众号:AI庭云君@CSDN:https://yetingyun.blog.csdn.net/"""importjiebaimportrefromstylecloudimportgen_stylecloudfromPILimportImageimportnumpyasnpwithopen('./圣诞素材/Christmas.txt',encoding="utf-8")asf:data=f.read()#文本预处理去除一些无用的字符只提取出中文出来new_data=re.findall('[\u4e00-\u9fa5]+',data,re.S)new_data="/".join(new_data)#文本分词精确模式seg_list_exact=jieba.cut(new_data,cut_all=False)#加载停用词withopen('stop_words.txt',encoding='utf-8')asf:#获取每一行的停用词添加进集合con=f.read().split('\n')stop_words=set()foriincon:stop_words.add(i)#列表解析式去除停用词和单个词result_list=[wordforwordinseg_list_exactifwordnotinstop_wordsandlen(word)>1]print(result_list)#个人推荐使用的palette配色方案效果挺好看其他测试过感觉一般~~#colorbrewer.qualitative.Dark2_7#cartocolors.qualitative.Bold_5#colorbrewer.qualitative.Set1_8gen_stylecloud(text=''.join(result_list),#文本数据size=600,#词云图大小font_path=r'./font/猫啃网糖圆体.ttf',#中文词云显示需要设置字体icon_name="fasfa-tree",#图标output_name='./results/圣诞树06.png',#输出词云图名称palette='cartocolors.qualitative.Bold_5',#选取配色方案)

效果如下所示:

Python如何制作圣诞树和圣诞树词云

四、彩蛋

在逛 Gitee 上还发现有人上传了 exe 可以直接生成圣诞树(貌似是C#做的?),效果如下所示:

Python如何制作圣诞树和圣诞树词云

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python如何制作圣诞树和圣诞树词云的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:C#中主要语言区域有哪些下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18