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

 更新时间:2021年12月24日 10:47:10   作者:叶庭云  
本文主要介绍了利用Python制作三种不同形状的圣诞树和圣诞树词云,文中的示例代码讲解详细,对我们学习Python有一定的帮助,快跟随小编一起学习吧

一、前言

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

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

二、Python画圣诞树

1. 圣诞树1号

# -*- coding: UTF-8 -*-
"""
@Author  :叶庭云
@公众号   :AI庭云君
@CSDN    :https://yetingyun.blog.csdn.net/
"""
import turtle

screen = 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, 0

for 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()

效果如下:

2. 圣诞树2号

# -*- coding: UTF-8 -*-
"""
@Author  :叶庭云
@公众号  :AI庭云君
@CSDN    :https://yetingyun.blog.csdn.net/
"""
from turtle import *
import turtle
import random

n = 100.0

speed(96)

turtle.setup(width=800, height=720)
# 用screensize设置的是画布大小及背景色
screensize(800, 800, "White")
left(90)
forward(3 * n)
color("red", "yellow")    # 五角星的颜色
begin_fill()
left(126)

for i in range(5):
    forward(n / 5)
    right(144)
    forward(n / 5)
    left(72)
end_fill()
right(126)

# 圣诞树颜色
color("#00CC00")
backward(n * 4.8)


def tree(d, s):
    if d <= 0:
        return
    forward(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)

for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if random.randint(0, 1) == 0:
        color('#FF0066')
    else:
        color('#FF6600')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)

turtle.mainloop()

效果如下:

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

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


drawsnow()  # 调用画雪花的方法

3. 圣诞树3号

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

三、Python制作圣诞树词云

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

12篇差不多够了吧

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

# -*- coding: UTF-8 -*-
"""
@Author   : 叶庭云
@公众号    : AI庭云君
@CSDN     : https://yetingyun.blog.csdn.net/
"""

import jieba
import re
from stylecloud import gen_stylecloud
from PIL import Image
import numpy as np


with 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_8
gen_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#做的?),效果如下所示:

地址:https://gitee.com/lengfengya/christmas-tree?_from=gitee_search 

以上就是Python制作圣诞树和圣诞树词云的详细内容,更多关于Python圣诞树词云的资料请关注脚本之家其它相关文章!

相关文章

  • Python中collections.Counter()的具体使用

    Python中collections.Counter()的具体使用

    本文主要介绍了Python中collections.Counter()的具体使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • python--pip--安装超时的解决方案

    python--pip--安装超时的解决方案

    这篇文章主要介绍了python--pip--安装超时的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • python groupby函数实现分组选取最大值与最小值

    python groupby函数实现分组选取最大值与最小值

    这篇文章主要介绍了python groupby函数实现分组选取最大值与最小值,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Tensorflow简单验证码识别应用

    Tensorflow简单验证码识别应用

    这篇文章主要为大家详细介绍了Tensorflow简单验证码识别应用的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • python不带重复的全排列代码

    python不带重复的全排列代码

    输入起始数字和结束数字将数组全排列,需要的朋友可以参考下
    2013-08-08
  • Python异步通信模块asyncore解读

    Python异步通信模块asyncore解读

    这篇文章主要介绍了Python异步通信模块asyncore的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Python实现上传Minio和阿里Oss文件

    Python实现上传Minio和阿里Oss文件

    这篇文章主要介绍了如何通过Python上传Minio和阿里OSS文件,文中的示例代码介绍得很详细,对我们的工作和学习都有一定的价值,感兴趣的小伙伴可以了解一下
    2021-12-12
  • 在Pycharm中使用GitHub的方法步骤

    在Pycharm中使用GitHub的方法步骤

    这篇文章主要介绍了在Pycharm中使用GitHub的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • python跳出双层for循环的解决方法

    python跳出双层for循环的解决方法

    今天小编就为大家分享一篇python跳出双层for循环的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python threading模块condition原理及运行流程详解

    Python threading模块condition原理及运行流程详解

    这篇文章主要介绍了Python threading模块condition原理及运行流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10

最新评论