↑↑↑关注后"
星标
"天池大数据科研平台
官方的月饼突然不香了,十一画月饼过云上中秋Python入门系列活动完美收官。天池Python入门系列赛是针对数据新人开设的实战练习专场,以有趣主题作为实践场景,提供详尽入门教程以及DSW算力资源,手把手教你学习python语法。天池希望此赛事能成为高校备受热捧的python实战课程,帮助更多学生掌握python技能。
十一上线的画月饼过云上中秋活动赛事一共290个队伍参与,官方提供的baseline被
Fork
了331次,一共评选出了25位人气月饼奖和10位最赞月饼奖。
大家大开脑洞,积极创新,"研发"出了各式各样的月饼,有美丽花纹的,有中文月饼,有NLP功能的月饼,有民大专属月饼等等,快来尝尝吧~
赛事地址:https://tianchi.aliyun.com/competition/entrance/531836/introduction
首先由
UnKnownCode0818
选手开发出一款能够在月饼上绘制中文(中文显示)而且能批量生产(自由设置月饼个数、文字内容)+打包(将图片转为GIF)的
月饼机器
。
#首先就是导入包,缺少的包自己安装一下
#包名后面的操作,是用什么源下载,直接影响下载速度
#我这里使用的是阿里云源
#pip install 包名 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
import os
from PIL import Image
import 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 FontProperties
from matplotlib import font_manager as fm, rcParams
import matplotlib as plt
import matplotlib.pyplot as plt
import random
import imageio
#这里是为了防止中文丢失
plt.rcParams['font.sans-serif']='SimHei'
#上面的都经过测试,全部没有问题
#第一个参数是设定月饼图片里面的核心名字,比如:晴天月饼,五仁月饼,榴莲月饼
#第二个参数就是月饼下面那一段字
#第三个参数就是月饼png图片的名字格式,比如规定了我要生成3个月饼,我传入的格式是Moon,那么最后生成的月饼图片名字就是Moon1.Moon2,Moon3
#第四个就是我生成的月饼数量,用来计算月饼到底生成到了那一个
def Moon( Mooncakename:str, Mooncakeintroduction:str, pngname:str, number:int ):
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='orange', linewidth=4) ##ec为线条颜色,可以自由替换
#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)
circle = Circle((0,0), R, ec='orange', fc='white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
wedge1 = Wedge([-2, 2], R-5, 90, 180,
ec='orange', fc=r'white', linewidth=4) ##ec为线条颜色,fc为填充颜色,可以自由替换
wedge2 = 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)
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)
#number = (int)(input("你想要几个月饼"))
plt.axis('off')
#name = (str)(input("月饼名字:"))
#Introduction = (str)(input("介绍或祝福:"))
#fname后面需要配置你的字体所在位置,若是在本目录,则直接写入Alibaba-PuHuiTi-Medimu.tty就行
#不过我还是建议写总目录,因为防止这个py文件转移位置了之后使用不了
font_set = FontProperties(fname=r"/data/nas/workspace/jupyter/download/Alibaba-PuHuiTi-Medium.ttf", size=24) ##可以自由下载字体使用
#这边一开始的程序在第一个text里面没有fontproperties=font_set,导致我一直报错,所文字丢失,所以这边我记录一下
#而且这边我的text标题变成了Moocakename,这就实现自定义月饼标题了
#第二个也是一样,自定义月饼下面的内容
plt.text(-15, -2.5, Mooncakename, fontproperties=font_set, bbox=dict(boxstyle='square', fc="w", ec='orange', linewidth=4), fontsize=50, color='orange') ##ec为线条颜色,color为字体颜色,可以自由替换
plt.text(-28, -33, Mooncakeintroduction, fontproperties=font_set, fontsize=30, color='#aa4a30')
plt.ylim([-35, 35])
plt.xlim([-35, 35])
fig = plt.gcf()
#显示每个月饼制作完成之后的样子(需要时开启)
#fig.show()
#MoonCakename = "MoonCake" + str(i+1) + ".png"
#MoonCakenamelist = list()
#MoonCakenamelist.append(MoonCakename)
#这边是设置保存的月饼名字以及保存月饼图片
fig.savefig(pngname + str(number+1) + ".png", dpi=100)
#这个函数是用来生成gif图片的,使用的是imageio模块库
#第一个是下面的main方法里面加载的文件名字,因为生成gif图,需要读取之前生成的图片,所以这就是为什么我上面的函数需要加一个图片格式的参数了
#第二个是gif的名字
#第三个是每张月饼图,在gif图里面显示的秒数,1就是1秒
def create_gif(image_list, gif_name, durationnumber):
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=durationnumber)
return
if __name__ == '__main__':
number = (int)(input("你想要几个月饼:"