SoFunction
Updated on 2024-11-17

Python to make Christmas trees and Christmas tree word clouds

I. Preface

Christmas celebrations and gift-giving seem to be a popular global habit now~

This article uses Python to make Christmas trees and word clouds, teaching you a variety of methods, the code runs directly on, learn to take it to the people you want to bless it~~~

II. Python Drawing Christmas Trees

1. Christmas Tree 1

# -*- coding: UTF-8 -*-
"""
@Author :Ye Tingyun
@public number :AI Tingyun
@CSDN :/
"""
import turtle

screen = ()
(800, 600)
circle = ()
('circle')
('red')
('fastest')
()
square = ()
('square')
('green')
('fastest')
()
(0, 280)
()
k, j = 0, 0

for i in range(1, 17):
    y = 30 * i
    for j in range(i - k):
        x = 30 * j
        (x, -y + 280)
        ()
        (-x, -y + 280)
        ()
    if i % 4 == 0:
        x = 30 * (j + 1)
        ('red')
        (-x, -y + 280)
        ()
        (x, -y + 280)
        ()
        k += 2
    if i % 4 == 3:
        x = 30 * (j + 1)
        ('yellow')
        (-x, -y + 280)
        ()
        (x, -y + 280)
        ()

('brown')
for i in range(17, 20):
    y = 30 * i
    for j in range(3):
        x = 30 * j
        (x, -y + 280)
        ()
        (-x, -y + 280)
        ()
        
()

The effect is as follows:

2. Christmas Tree 2

# -*- coding: UTF-8 -*-
"""
@Author :Ye Tingyun
@public number :AI Tingyun
@CSDN :/
"""
from turtle import *
import turtle
import random

n = 100.0

speed(96)

(width=800, height=720)
# The canvas size and background color are set with screensize.
screensize(800, 800, "White")
left(90)
forward(3 * n)
color("red", "yellow")    # The color of the pentagram
begin_fill()
left(126)

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

# Christmas tree colors
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 * ()
    b = 10 - 20 * ()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if (0, 1) == 0:
        color('#FF0066')
    else:
        color('#FF6600')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)

()

The effect is as follows:

You can also change the background and add snow effects! As shown below:

# Drawing snowflakes key code
def drawsnow():      # Define the method of drawing snowflakes
    ()           # Hide the penny, ht=hideturtle #
    (2)     # Define pen size
    for i in range(200):     # Draw as many snowflakes
        ("white")  # Define the brush color as white, which is actually snowflakes as white
        ()  # Pen up, pu=penup
        ((-350, 350))  # Define the x-coordinate, randomly selected from -350 to 350
        ((-100, 350))  # Define the y-coordinate, noting that snowflakes don't generally fall on the ground, so they don't start with too small a vertical coordinate axis
        ()    # Drop the pen, pd=pendown #
        dens = 6  # The number of snowflake petals is set to 6
        snowsize = (1, 10)  # Define snowflake size
        for j in range(dens):        # That's six, that's five times, that's a snowflake pentagram #
            # (int(snowsize)) #int() takes integers
            (int(snowsize))
            (int(snowsize))
            # (int(snowsize)) # notice there is no bd=backward but there is fd=forward, small bug
            (int(360 / dens))  # Turning angle


drawsnow()  # Calls to draw snowflakes

3. Christmas Tree 3

Draw a beautiful Christmas tree! The code is too long to post in the text for space limitations, and the effect is shown below:

III. Python to make a Christmas tree word cloud

Do word cloud have to have keyword material, this goes to Baidu library college version to download some Christmas blessing article ~~

Twelve is almost enough.

Next up is the Python code !!!!!!

# -*- coding: UTF-8 -*-
"""
@Author : Ye Tingyun
@public_number : AI Tingyun
@CSDN : /
"""

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


with open('. /Christmas material/', encoding="utf-8") as f:
    data = ()

# Text preprocessing Remove some useless characters and extract only the Chinese characters.
new_data = ('[\u4e00-\u9fa5]+', data, )
new_data = "/".join(new_data)

# Text Segmentation Exact Mode
seg_list_exact = (new_data, cut_all=False)

# Load stop words
with open('stop_words.txt', encoding='utf-8') as f:
    # Get the stop words for each line and add them to the set.
    con = ().split('\n')
    stop_words = set()
    for i in con:
        stop_words.add(i)

# List parsing Remove stop words and single words
result_list = [word for word in seg_list_exact if word not in stop_words and len(word) > 1]
print(result_list)

# I personally recommend the palette color scheme # # It looks good # # The others I've tested # # I feel so-so #
# .Dark2_7
# .Bold_5
# .Set1_8
gen_stylecloud(
    text=' '.join(result_list),                   # Text data
    size=600,                                     # Word cloud size
    font_path=r'. /font/cat-nibbles.net-sugar-circular.ttf',          # Chinese word cloud Display requires font setting
    icon_name = "fas fa-tree",                    # Icons
    output_name='. /results/christmas-tree',          # Output word cloud names
    palette='.Bold_5',     # Selection of color schemes
)

The effect is shown below:

IV. Colored eggs

While browsing Gitee, I also found that someone uploaded an exe to generate a Christmas tree directly (looks like it's done in C#?). The result is shown below:

Address:/lengfengya/christmas-tree?_from=gitee_search 

Above is the detailed content of Python to make Christmas tree and Christmas tree word cloud, more information about Python Christmas tree word cloud please pay attention to my other related articles!