0
点赞
收藏
分享

微信扫一扫

中秋节之Python绘制许愿树和月饼(打开程序员世界的大门)


好的成长总是游走在舒适区边缘,始终在伸拉区练习。就是首先去追求那些能够得到的东西,或则稍微努力就能得到的东西。下面这些程序抛转引玉希望能作为大家迈入程序世界的钥匙。

为了提升氛围大家先欣赏下软件工程版的青花瓷

中秋节之Python绘制许愿树和月饼(打开程序员世界的大门)_python


青花瓷(软件工程版)


好了下面进入正题 

目录

​​一、利用Python编写的中秋许愿树 ​​

​​二、自定义中秋月饼绘制 ​​

一、利用Python编写的中秋许愿树 


中秋许愿树


import turtle
import random


def love(x, y): # 在(x,y)处画爱⼼

lv = turtle.Turtle()
lv.hideturtle()
lv.up()
lv.goto(x, y) # 定位到(x,y)


def curvemove():
for i in range(20):
lv.right(10)
lv.forward(2)

lv.color('red', 'pink')
lv.speed(10000000)
lv.pensize(1)
lv.down()
lv.begin_fill()
lv.left(160)
lv.forward(22)
curvemove()
lv.left(120)
curvemove()
lv.forward(22)
lv.write("中秋节快乐", font=("Arial", 12, "normal"), align="center") # 写上表⽩的⼈的名字
lv.left(140) # 画完复位
lv.end_fill()


def tree(branchLen, t):
if branchLen > 5:
if branchLen < 20:
t.color("green")
t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
t.down()
t.down()
t.forward(branchLen)
love(t.xcor(), t.ycor()) # 传输现在turtle的坐标
t.up()
t.backward(branchLen)
t.color("brown")
return

t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
t.down()
t.forward(branchLen)

# 以下递归
ang = random.uniform(15, 45)
t.right(ang)
tree(branchLen - random.uniform(12, 16), t) # 随机决定减⼩长度
t.left(2 * ang)
tree(branchLen - random.uniform(12, 16), t) # 随机决定减⼩长度
t.right(ang)
t.up()
t.backward(branchLen)

myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(1000)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
myWin.exitonclick()

二、自定义中秋月饼绘制 

中秋节之Python绘制许愿树和月饼(打开程序员世界的大门)_保存图片_02

from numpy import sin, cos, pi
import matplotlib.pyplot as plt
from matplotlib.patches import Arc, Circle, Wedge
from matplotlib.font_manager import FontProperties



num=5
list={'红色':'red','黄色':'yellow','淡蓝':'lightcyan','粉红':'pink','绿色':'greenyellow','橘子色':'orange','天空蓝':'aqua',
'土黄色':'wheat','紫色':'deeppink','灰色':'lightgrey','深黄色':'orangered','深紫色':'orchid'} #颜色的样式


class MoonCake(): #定义一个类
def __init__(self,pattern,center,roundt,edge,sector,largefan,largefan_0,blessing,blessing_0,number): #对数据进行封装
self.pattern = pattern #月饼边缘花纹的颜色
self.center = center #月饼主体圆,边的颜色
self.roundt = roundt #月饼主体圆实部的颜色
self.edge =edge #请输入4个大的扇形花纹的颜色
self.sector = sector #请输入4个大的扇形花纹实部的颜色
self.largefan = largefan #请输入4个小的扇形花纹的颜色
self.largefan_0 = largefan_0 #请输入4个小的扇形花纹实部的颜色
self.blessing = blessing #请输入祝福语
self.blessing_0 = blessing_0 #请输入祝福语的颜色
self.number = number #请输入保存图片的序号



def lucky(self):
length = 20
R = 3**0.5*length/(3**0.5*cos(pi/12)-sin(pi/12))
r = 2*sin(pi/12)*R/3**0.5

arc1 = Arc([0, length], width=2*r, height=2*r,
angle=0, theta1=30, theta2=150, ec=str(self.pattern),linewidth=4) #ec为线条颜色,fc为填充颜色,可以自由替换


arc2 = Arc([-length/2, length/2*3**0.5], width=2*r, height=2*r,
angle=0, theta1=60, theta2=180, ec=str(self.pattern), linewidth=4)
arc3 = Arc([-length/2*3**0.5, length/2], width=2*r, height=2*r,
angle=0, theta1=90, theta2=210, ec=str(pattern), linewidth=4)
arc4 = Arc([-length, 0], width=2*r, height=2*r, angle=0, theta1=120, theta2=240, ec=str(self.pattern),linewidth=4) #ec为线条颜色,fc为填充颜色,可以自由替换
arc5 = Arc([-length/2*3**0.5, -length/2], width=2*r, height=2*r,
angle=0, theta1=150, theta2=270, ec=str(self.pattern), linewidth=4)
arc6 = Arc([-length/2, -length/2*3**0.5], width=2*r, height=2*r,
angle=0, theta1=180, theta2=300, ec=str(self.pattern), linewidth=4)
arc7 = Arc([0, -length], width=2*r, height=2*r, angle=0, theta1=210, theta2=330, ec=str(self.pattern),linewidth=4)
arc8 = Arc([length/2, -length/2*3**0.5], width=2*r, height=2*r,
angle=0, theta1=240, theta2=360, ec=str(self.pattern), linewidth=4)
arc9 = Arc([length/2*3**0.5, -length/2], width=2*r, height=2*r,
angle=0, theta1=270, theta2=390, ec=str(self.pattern), linewidth=4)
arc10 = Arc([length, 0], width=2*r, height=2*r, angle=0, theta1=300, theta2=420, ec=str(self.pattern), linewidth=4)
arc11 = Arc([length/2*3**0.5, length/2], width=2*r, height=2*r,
angle=0, theta1=330, theta2=450, ec=str(self.pattern), linewidth=4)
arc12 = Arc([length/2, length/2*3**0.5], width=2*r, height=2*r,
angle=0, theta1=0, theta2=120, ec=str(self.pattern), linewidth=4)

circle = Circle((0,0), R, ec=str(self.center), fc=str(self.roundt), linewidth=4)



wedge1 = Wedge([-2, 2], R-5, 90, 180,
ec=str(self.edge), fc=str(self.sector), linewidth=4)

wedge2 = Wedge([-5, 5], R-12, 90, 180, ec=str(self.largefan),
fc=str(self.largefan_0), linewidth=4) #ec为线条颜色,fc为填充颜色,可以自由替换
wedge3 = Wedge([-2, -2], R-5, 180, 270, ec=str(self.edge),
fc=str(self.sector), linewidth=4)
wedge4 = Wedge([-5, -5], R-12, 180, 270, ec=str(self.largefan),
fc=str(self.largefan_0), linewidth=4)
wedge5 = Wedge([2, -2], R-5, 270, 360, ec=str(self.edge),
fc=str(self.sector), linewidth=4)
wedge6 = Wedge([5, -5], R-12, 270, 360, ec=str(self.largefan),
fc=str(self.largefan_0), linewidth=4)
wedge7 = Wedge([2, 2], R-5, 0, 90, ec=str(self.edge), #ec为线条颜色,fc为填充颜色,可以自由替换
fc=str(self.sector), linewidth=4)
wedge8 = Wedge([5, 5], R-12, 0, 90, ec=str(self.largefan),
fc=str(self.largefan_0), linewidth=4)


art_list = [arc1, arc2, arc3, arc4, arc5, arc6, arc7, arc8, arc9, arc10, arc11, arc12]
art_list.extend([circle, wedge1, wedge2, wedge3, wedge4, wedge5, wedge6, wedge7, wedge8])
fig, ax = plt.subplots(figsize=(8,8))
ax.set_aspect('equal')
for a in art_list:
ax.add_patch(a)


plt.axis('off')
font_set = FontProperties(fname=r"Alibaba-PuHuiTi-Medium.ttf", size=15)
plt.text(-8.4, -7,'中秋\n快乐', bbox=dict(boxstyle='circle', fc="skyblue", ec='red', linewidth=4), fontproperties=font_set, fontsize=50, color='yellow') ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-43, -33, str(self.blessing),fontproperties=font_set, fontsize=30, color=str(self.blessing_0))
plt.ylim([-35, 35])
plt.xlim([-35, 35])
name='tset'+str(number)+'.png' #生成图片序号
plt.savefig(name) #保存图片

plt.show()

while num: #根据需要自己可以决定制作几个月饼
print('选择颜色,选择颜色输入一定要正确{}'.format(list))
print('不选择我给出的颜色也可以,你自定义选择的颜色输入一定要准确\n') #输入的格式一定要正确,不然会错
print('')
pattern = input('请输入月饼边缘花纹的颜色:') #制作月饼,并输入参数
center = input('请输入月饼主体圆,边的颜色:')
roundt = input('请输入月饼主体圆实部的颜色:')
edge = input('请输入4个大的扇形花纹的颜色:')
sector = input('请输入4个大的扇形花纹实部的颜色:')
largefan = input('请输入4个小的扇形花纹的颜色:')
largefan_0 = input('请输入4个小的扇形花纹实部的颜色:')
blessing = input('请输入祝福语:')
blessing_0 = input('请输入祝福语的颜色:')
number = int(input('请输入保存图片的序号:'))
num = int(input('输入数字0结束月饼制作:'))

good=MoonCake(pattern,center,roundt,edge,sector,largefan,largefan_0,blessing,blessing_0,number)
good.lucky()

字体下载地址: 

http://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/Python/miniproject/01_draw_moon_cake/Alibaba-PuHuiTi-Medium.ttf

举报

相关推荐

0 条评论