Python制作圣誕樹和圣誕樹詞云
一、前言
圣誕節(jié)慶祝和送禮物貌似現(xiàn)在已經(jīng)成為全球流行的習(xí)慣~

本文利用 Python 制作圣誕樹和詞云,教會你多種方法,代碼直接運(yùn)行即可,學(xué)會拿去送給你想要祝福的人吧~~
二、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設(shè)置的是畫布大小及背景色
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()
效果如下:

還可以更改背景,加上下雪特效!如下所示:
# 畫雪花關(guān)鍵代碼
def drawsnow(): # 定義畫雪花的方法
t.ht() # 隱藏筆頭,ht=hideturtle
t.pensize(2) # 定義筆頭大小
for i in range(200): # 畫多少雪花
t.pencolor("white") # 定義畫筆顏色為白色,其實(shí)就是雪花為白色
t.pu() # 提筆,pu=penup
t.setx(r.randint(-350, 350)) # 定義x坐標(biāo),隨機(jī)從-350到350之間選擇
t.sety(r.randint(-100, 350)) # 定義y坐標(biāo),注意雪花一般在地上不會落下,所以不會從太小的縱座軸開始
t.pd() # 落筆,pd=pendown
dens = 6 # 雪花瓣數(shù)設(shè)為6
snowsize = r.randint(1, 10) # 定義雪花大小
for j in range(dens): # 就是6,那就是畫5次,也就是一個雪花五角星
# t.forward(int(snowsize)) #int()取整數(shù)
t.fd(int(snowsize))
t.backward(int(snowsize))
# t.bd(int(snowsize)) #注意沒有bd=backward,但有fd=forward,小bug
t.right(int(360 / dens)) # 轉(zhuǎn)動角度
drawsnow() # 調(diào)用畫雪花的方法

3. 圣誕樹3號
畫一顆漂亮的圣誕樹!!代碼過長,限于篇幅不貼在文中,效果如下所示:

三、Python制作圣誕樹詞云
做詞云得有關(guān)鍵詞素材,這就去百度文庫高校版下載一些圣誕祝福文章~~

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()
# 文本預(yù)處理 去除一些無用的字符 只提取出中文出來
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:
# 獲取每一行的停用詞 添加進(jìn)集合
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), # 文本數(shù)據(jù)
size=600, # 詞云圖大小
font_path=r'./font/貓啃網(wǎng)糖圓體.ttf', # 中文詞云 顯示需要設(shè)置字體
icon_name = "fas fa-tree", # 圖標(biāo)
output_name='./results/圣誕樹06.png', # 輸出詞云圖名稱
palette='cartocolors.qualitative.Bold_5', # 選取配色方案
)
效果如下所示:

四、彩蛋
在逛 Gitee 上還發(fā)現(xiàn)有人上傳了 exe 可以直接生成圣誕樹(貌似是C#做的?),效果如下所示:

地址:https://gitee.com/lengfengya/christmas-tree?_from=gitee_search?
以上就是Python制作圣誕樹和圣誕樹詞云的詳細(xì)內(nèi)容,更多關(guān)于Python圣誕樹詞云的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python異步的ASGI與Fast Api實(shí)現(xiàn)
本文主要介紹了python異步的ASGI與Fast Api實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
pycharm配置python 設(shè)置pip安裝源為豆瓣源
這篇文章主要介紹了pycharm配置python 設(shè)置pip安裝源為豆瓣源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
解決Windows下PowerShell無法進(jìn)入Python虛擬環(huán)境問題
這篇文章主要介紹了解決Windows下PowerShell無法進(jìn)入Python虛擬環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
分析Python的Django框架的運(yùn)行方式及處理流程
這篇文章主要介紹了分析Python的Django框架的運(yùn)行方式及處理流程,本文對于Django框架的機(jī)制總結(jié)得非常之直觀精煉,極力推薦!需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法,涉及Python字符串遍歷、轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
Python網(wǎng)絡(luò)請求之Requests庫的高級功能運(yùn)用
python中not not x 與bool(x) 的區(qū)別

