1、原材料
1.1 花灯纸
如下所示,还可以加上自己喜欢的图案、文字等。
2.2 Python环境和模块
一台安装了Python环境的电脑,Python环境需要安装以下模块。
numpy
pillow
wxgl
如果没有上述模块,请参考下面的命令安装。
pip install numpy
pip install pillow
pip install wxgl
2、源代码
#元宵节就要到了,花灯要不要来一盏?3D的那种
# -*- coding: utf-8 -*-
import numpy as np
from PIL import Image
import wxgl.wxplot as plt
im = np.array(Image.open('D:/Lantern/venv/res/paper.png'))/255
rows, cols, deep = im.shape
#np.pi=π rows=灯笼高 cols=灯笼底圆的周长
r, h = 1, 2*np.pi*rows/cols
# r=1.5 h=2.66
#制作半径1个单位、高度2.668个单位的圆筒状龙骨了。
#将0到2π分成942份
theta = np.linspace(0, 2*np.pi, cols)
x = r*np.cos(theta)
y = r*np.sin(theta)
#将0到h=2.66分成400份
z = np.linspace(0, h, rows)
xs = np.tile(x, (rows,1))
ys = np.tile(y, (rows,1))
zs = z.repeat(cols).reshape((rows,cols))
xs1 = np.tile(x, (rows,1))+3
xs2 = np.tile(x, (rows,1))-3
ys1 = np.tile(y, (rows,1))+3
ys2 = np.tile(y, (rows,1))-3
#制作旋转叶轮
theta = np.linspace(0, 2*np.pi, 18, endpoint=False)
x[2::3] = x[1::3]
x[1::3] = 0
y[2::3] = y[1::3]
y[1::3] = 0
z = np.ones(18) * h * 0.9
vs = np.stack((x,y,z), axis=1)
vs1 = np.stack((x+3,y,z), axis=1)
vs2 = np.stack((x-3,y,z), axis=1)
vs3 = np.stack((x,y+3,z), axis=1)
vs4 = np.stack((x,y-3,z), axis=1)
#每隔10个点抽取1个点,用mesh的方法画出龙骨形状
plt.mesh(xs, ys, zs, im[::-1])
plt.mesh(xs1, ys, zs, im[::-1])
plt.mesh(xs2, ys, zs, im[::-1])
plt.mesh(xs, ys1, zs, im[::-1])
plt.mesh(xs, ys2, zs, im[::-1])
#旋转叶轮画成红色
plt.surface(vs, color='#C03000', method='T', mode='FCBL', alpha=0.8)
plt.surface(vs1, color='#C03000', method='T', mode='FCBL', alpha=0.8)
plt.surface(vs2, color='#C03000', method='T', mode='FCBL', alpha=0.8)
plt.surface(vs3, color='#C03000', method='T', mode='FCBL', alpha=0.8)
plt.surface(vs4, color='#C03000', method='T', mode='FCBL', alpha=0.8)
#白色照明灯
plt.sphere((0,0,h*0.4), 0.4, '#FFFFFF', slices=60, mode='FCBC')
plt.sphere((3,0,h*0.4), 0.4, '#FFFFFF', slices=60, mode='FCBC')
plt.sphere((-3,0,h*0.4), 0.4, '#FFFFFF', slices=60, mode='FCBC')
plt.sphere((0,3,h*0.4), 0.4, '#FFFFFF', slices=60, mode='FCBC')
plt.sphere((0,-3,h*0.4), 0.4, '#FFFFFF', slices=60, mode='FCBC')
#红线灯笼线
plt.plot((0,0), (0,0), (0.4*h, 1.5*h), width=3.0, style='solid', cmap='hsv', caxis='z')
plt.plot((3,3), (0,0), (0.4*h, 1.5*h), width=3.0, style='solid', cmap='hsv', caxis='z')
plt.plot((-3,-3), (0,0), (0.4*h, 1.5*h), width=3.0, style='solid', cmap='hsv', caxis='z')
plt.plot((0,0), (3,3), (0.4*h, 1.5*h), width=3.0, style='solid', cmap='hsv', caxis='z')
plt.plot((0,0), (-3,-3), (0.4*h, 1.5*h), width=3.0, style='solid', cmap='hsv', caxis='z')
print(plt.title.__doc__)
#红色
plt.title(text='元宵节快乐!', size=64, color='red', pos=(0,0,0),align='center',weight='bold')
plt.show(rotation='h-')
3、效果
到此这篇关于Python制作旋转花灯祝大家元宵节快乐的文章就介绍到这了,更多相关Python旋转花灯内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!