举头望明月,低头思故乡,不知道为什么,现在总是对儿时的事有感而发,回不到的过去,唯有珍惜当下,中秋快乐.

1、首先我们需要导入画图和数据计算的相关库。

// An highlighted blockimport numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
import matplotlib.patches as mpatch
from matplotlib.patches import Arc, Circle, Wedge
from matplotlib.collections import PatchCollection
from matplotlib.font_manager import FontPropertiesvar foo = 'bar';

2、为了在图片上显示中文,需要先加载Alibaba-PuHuiTi-Medium字体下载地址:

// An highlighted blockhttp://tianchi-media.oss-cn-beijing.aliyuncs.com/DSW/Python/miniproject/01_draw_moon_cake/Alibaba-PuHuiTi-Medium.ttfvar foo = 'bar';

3、然后设置3个变量,这3个变量的用途到后面会有解释:

// An highlighted blocklength = 20
R = 3**0.5*length/(3**0.5*cos(pi/12)-sin(pi/12))
r = 2*sin(pi/12)*R/3**0.5var foo = 'bar';

4、接下来我们要画弧线,弧线是月饼边缘的花纹:

// An highlighted blockarc1 = Arc([0, length], width=2*r, height=2*r, angle=0, theta1=30, theta2=150, ec='orange', linewidth=4)  #ec为线条颜色,可以自由替换var foo = 'bar';

5、其中 [0, length] 是这个Arc弧线的圆心,因为这个Arc类是一个椭圆形的一部分,所以其包含圆心、横轴、纵轴等参数,length变量就是arc1圆心的纵坐标,width=2r 和 height=2r 就说明这个椭圆的纵轴和横轴长度相等,其是一个圆,而不是椭圆,变量 r 就是这个圆的半径。angle=0 就是我们不需要对这个圆进行旋转,theta1=30 和 theta2=150 是指明这个弧线的起始角度和终止角度,这里的角度分别是30度和150度。而 ec=‘orange’ 和linewidth=4 分别指边线的颜色和线条宽度,颜色我们设为橙色,宽度为4。而这些弧线的效果图如图2所示,在图中笔者标出了arc1、arc5和arc9这3条弧线的位置,所有弧线按照逆时针方向排列。这些位置大同小异,大家只要计算好其各自的位置参数就行,这里就不再赘述了。

接下来我们要画剩下的11条弧线

// An highlighted block#arc1 = Arc([0, length], width=2*r, height=2*r, angle=0, theta1=30, theta2=150, ec='orange', linewidth=4)
arc2 = Arc([-length/2, length/2*3**0.5], width=2*r, height=2*r, angle=0, theta1=60, theta2=180, ec='orange', linewidth=4)
arc3 = Arc([-length/2*3**0.5, length/2], width=2*r, height=2*r, angle=0, theta1=90, theta2=210, ec='orange', linewidth=4)
arc4 = Arc([-length, 0], width=2*r, height=2*r, angle=0, theta1=120, theta2=240, ec='orange', linewidth=4)
arc5 = Arc([-length/2*3**0.5, -length/2], width=2*r, height=2*r, angle=0, theta1=150, theta2=270, ec='orange', linewidth=4)
arc6 = Arc([-length/2, -length/2*3**0.5], width=2*r, height=2*r,angle=0, theta1=180, theta2=300, ec='orange', linewidth=4)
arc7 = Arc([0, -length], width=2*r, height=2*r, angle=0, theta1=210, theta2=330, ec='orange', linewidth=4)
arc8 = Arc([length/2, -length/2*3**0.5], width=2*r, height=2*r,angle=0, theta1=240, theta2=360, ec='orange', linewidth=4)
arc9 = Arc([length/2*3**0.5, -length/2], width=2*r, height=2*r,angle=0, theta1=270, theta2=390, ec='orange', linewidth=4)
arc10 = Arc([length, 0], width=2*r, height=2*r, angle=0, theta1=300, theta2=420, ec='orange', linewidth=4)
arc11 = Arc([length/2*3**0.5, length/2], width=2*r, height=2*r,angle=0, theta1=330, theta2=450, ec='orange', linewidth=4)
arc12 = Arc([length/2, length/2*3**0.5], width=2*r, height=2*r,angle=0, theta1=0, theta2=120, ec='orange', linewidth=4)var foo = 'bar';

6、然后我们再画一个圆,这个圆是月饼的主体部分,其圆心和整个月饼的中心是重合的,圆心位置我们选在了坐标原点,代码如下:

// An highlighted blockcircle = Circle((0,0), R, ec='orange', fc='white', linewidth=4) #ec为线条颜色,fc为填充颜色,可以自由替换var foo = 'bar';

7、这里变量R就是这个大圆的半径,边线颜色(ec)设置为橙色,表面颜色设为白色。 接下来我们再画上一些花纹,这些花纹主要由8个扇形组成,其中有4个大的扇形和4个小的扇形,我们先画一个,代码如下:

// An highlighted blockwedge1 = Wedge([-2, 2], R-5, 90, 180,ec='orange', fc=r'white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换var foo = 'bar';

8、其中[-2, 2]是这个Wedge所代表的圆的圆心,R-5 是其半径,90 和180分别代表起始和终止角度,ec、fc和linewidth这些都和前面Arc类的用法相似,这8个扇形的效果图如图4所示。在图中笔者标出了wedge1、wedge2、wedge5和wedge6的位置,所有扇形按照逆时针方向放置。 了解以后我们把剩下的部分给画完

// An highlighted blockwedge2 = Wedge([-5, 5], R-12, 90, 180, ec='orange',fc=r'white', linewidth=4)
wedge3 = Wedge([-2, -2], R-5, 180, 270, ec='orange', fc=r'white', linewidth=4)
wedge4 = Wedge([-5, -5], R-12, 180, 270, ec='orange', fc=r'white', linewidth=4)
wedge5 = Wedge([2, -2], R-5, 270, 360, ec='orange', fc=r'white', linewidth=4)
wedge6 = Wedge([5, -5], R-12, 270, 360, ec='orange',fc=r'white', linewidth=4)
wedge7 = Wedge([2, 2], R-5, 0, 90, ec='orange', fc=r'white', linewidth=4)
wedge8 = Wedge([5, 5], R-12, 0, 90, ec='orange',fc=r'white', linewidth=4)var foo = 'bar';

9、最后我们再把自己取好的名称加上,因为代码不多,所以把剩余所有代码都写在这里:

// An highlighted blockart_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=12) ##可以自由下载字体使用
plt.text(-15, -2.5, 'luckyCh', bbox=dict(boxstyle='square', fc="w", ec='orange', linewidth=4),  fontsize=50, color='orange') ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-28, -33, 'Python画月饼,千里共禅娟',fontproperties=font_set, fontsize=30, color='#aa4a30')
plt.ylim([-35, 35])
plt.xlim([-35, 35])plt.show()var foo = 'bar';


10、art_list是我们设置的一个list变量,里面放有arc1到arc12这12个图形,然后再把circle以及8个扇形都加进去。ax.set_aspect(‘equal’)是设置整个图形x轴和y轴同比例,for a in art_list: ax.add_patch(a) 是把art_list中所有图形加入到画布当中。而plt.text这行代码中,-18和-2.5是这个text的左下角的坐标,fontfamily 和 fontsize 分别是文字的字体和大小,bbox 是设置text边框的格式,这些参数也都和前面讲过的很多参数差不多,这里不再赘述。plt.ylim 和plt.xlim是设置整个画布的坐标范围。最终效果如图1所示。

当然我们也可以改变图片的颜色,比如设置成蓝色的,这个完全可以根据个人爱好而定。

百变风味月饼代码

// An highlighted blockimport numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
import matplotlib.patches as mpatch
from matplotlib.patches import Arc, Circle, Wedge
from matplotlib.collections import PatchCollection
from matplotlib.font_manager import FontPropertiesnum=5
list={'红色':'red','黄色':'yellow','淡蓝':'lightcyan','粉红':'pink','绿色':'greenyellow','橘子色':'orange','天空蓝':'aqua','土黄色':'wheat','紫色':'deeppink','灰色':'lightgrey','深黄色':'orangered','深紫色':'orchid'}                    #颜色的样式class Dog():                           #定义一个类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.5arc1 = 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=Dog(pattern,center,roundt,edge,sector,largefan,largefan_0,blessing,blessing_0,number)good.lucky()#需要先看懂注释才能运行代码.var foo = 'bar';


公众号:彼岸星空

用python画百变风味月饼相关推荐

  1. python绘制回形纹_用python画百变风味月饼

    举头望明月,低头思故乡,不知道为什么,现在总是对儿时的事有感而发,回不到的过去,唯有珍惜当下,中秋快乐. 1.首先我们需要导入画图和数据计算的相关库. import numpy as np from ...

  2. 用Python玩百变人脸!趣味容颜

    1 人脸 人脸乃人之门面,在这看「颜」的时代,「颜」即正义. 出门前,都会特意看看自己的脸打扮得是否满意,而没有人会特意看看自己的脚趾窝是否干净. 生活中,人脸是人们关注的重点.技术上,人脸同样也是研 ...

  3. 用Python玩百变人脸!趣味容颜,ALAE 人脸玩出新高度!

    1 人脸 人脸乃人之门面,在这看「颜」的时代,「颜」即正义. 出门前,都会特意看看自己的脸打扮得是否满意,而没有人会特意看看自己的脚趾窝是否干净. 生活中,人脸是人们关注的重点.技术上,人脸同样也是研 ...

  4. python画圆形螺旋线_中秋节到了,送你一个Python做的Crossin牌“月饼”

    明天是难得一见的国庆中秋双节合一,在这里除了祝大家节日快乐之外,我们还要送上一个"月饼"--当然这个月饼是不能吃的,因为它是用python做的.先给大家看一下效果图. 图1. 月饼 ...

  5. python画中秋月饼,用turtle海龟库画中秋月饼

    本python画月饼的代码封装比较灵活,可以自由调用,不懂的地方可以留言交流 2022新录制的绘制视频,祝大家中秋快乐. https://www.bilibili.com/video/BV1DP411 ...

  6. 用 Python 画个月饼,给大家助助兴

    中秋节到了,祝大家中秋节快乐!用 Python 画个月饼,给大家助助兴. 效果图 依赖项 pip install numpy pip install matplotlib 源代码 需要下载字体文件 A ...

  7. python画中秋的月亮_Python画月饼,云上过中秋,天池Python入门案例系列赛开启

    原标题:Python画月饼,云上过中秋,天池Python入门案例系列赛开启 阿里云天池推出了一个Python入门案例系列教程,在此之前他们还推出了一个Python基础训练营. 在天池龙珠计划Pytho ...

  8. Python画简单月饼(使用turtle)

    学习也要"逢其时". 今天是2021年9月21日,中秋节,准备用python画个月饼,看了很多资料后来选了一个简单的月饼,参考的原网址找不到了,感谢这位大神. 第一步:用函数说明画 ...

  9. 百变星君,Python变量两三事

    百变星君,Python变量两三事 什么是变量 变量就是不断变化的量.在Python中假设有一个变量A,它的值是233,我们可以写成 A=233 print(A) 得到结果233. 我们还可以将A赋值成 ...

最新文章

  1. python tcp不用循环监听_网络编程: TCP
  2. mysql_select按照指定的格式输出到文件
  3. golang 读写文件的四种方式
  4. 王道机试指南读后总结-6(动态规划等)
  5. Java 线程池的复用原理
  6. AcWing 9. 分组背包问题(分组背包模板)
  7. Lync Server 2013 实战系列之二:标准版-前期准备
  8. 7-107 通讯录排序 (20 分)
  9. 常用开发工具、网站、文章、博客、官网、资源、牛人等推荐(持续更新)
  10. linux: dirent.h 使用
  11. Java中list转map的常用方法
  12. 平板电脑安装软件_手机象棋软件手机、平板、电脑三平台同时安装!学棋涨棋必备...
  13. mbit职业测试软件,APESK瑞士荣格理论模型职业性格测试(非迫选模式比MBTI性格测试更人性化)(量表版本:V2015-3.1)...
  14. Windows 10 无法访问共享的解决办法大全
  15. 来了!SpringBoot从入门到入魔的私房教程!
  16. linux7开放svn,CentOS 7 下SVN的安装及基础配置介绍
  17. 计算机应用基础难点,计算机应用基础(本科)重、难点
  18. [转] C++中字符型变量的地址输出
  19. SDK接入(之Android Google Play内支付(in-app Billing)接入的细节
  20. 电信增值业务学习笔记13——增值业务管理

热门文章

  1. c语言指针引用数组元素,c语言——数组指针和通过指针引用数组元素的方法总结...
  2. C++基础知识 - 指针引用
  3. SDINDDH6-128G-ZA通用闪存存储器 - UFS WD/SD
  4. 毕业设计-机器视觉深度学习的视频去水印算法
  5. 用excel怎么求和的方式你知道哪些
  6. 解读Keras在ImageNet中的应用:详解5种主要的图像识别模型
  7. java arraylist .get_Java ArrayList get()方法与示例
  8. java arraylist 删除_Java ArrayList删除特定元素的方法
  9. 【中英双语】使用JavaScript 及Three.js开发3D网页游戏
  10. html字体竖排上下间距,css文字竖排显示 文字垂直布局