龙空技术网

平安喜乐 | 教你用Python制作圣诞树和词云

一个不秃头的 121

前言:

现在看官们对“python写一个礼物”大致比较讲究,大家都需要剖析一些“python写一个礼物”的相关资讯。那么小编同时在网上汇集了一些对于“python写一个礼物””的相关知识,希望小伙伴们能喜欢,你们一起来了解一下吧!

一、前言

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

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

二、Python画圣诞树

# -*- coding: UTF-8 -*-"""@Author  :叶庭云@公众号   :AI庭云君@CSDN    :;""import turtlescreen = 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, 0for i in range(1, 17):    y = 30 * i    for j in range(i - k):        x = 30 * j        square.goto(x, -y + 280)        square.stamp()        square.goto(-x, -y + 280)        square.stamp()    if i % 4 == 0:        x = 30 * (j + 1)        circle.color('red')        circle.goto(-x, -y + 280)        circle.stamp()        circle.goto(x, -y + 280)        circle.stamp()        k += 2    if i % 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')for i in range(17, 20):    y = 30 * i    for j in range(3):        x = 30 * j        square.goto(x, -y + 280)        square.stamp()        square.goto(-x, -y + 280)        square.stamp()        turtle.mainloop()

效果如下:

三、Python制作圣诞树词云

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

差不多够了吧

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

# -*- coding: UTF-8 -*-"""@Author   : 叶庭云@公众号    : AI庭云君@CSDN     : ;""import jiebaimport refrom stylecloud import gen_stylecloudfrom PIL import Imageimport numpy as npwith open('./圣诞素材/Christmas.txt', encoding="utf-8") as f:    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)# 加载停用词with open('stop_words.txt', encoding='utf-8') as f:    # 获取每一行的停用词 添加进集合    con = f.read().split('\n')    stop_words = set()    for i in con:        stop_words.add(i)# 列表解析式  去除停用词和单个词result_list = [word for word in seg_list_exact if word not in stop_words and len(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 = "fas fa-tree",                    # 图标    output_name='./results/圣诞树06.png',          # 输出词云图名称    palette='cartocolors.qualitative.Bold_5',     # 选取配色方案

效果如下所示:

四、彩蛋

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

标签: #python写一个礼物