1、 raw material
1.1 Lantern paper
As shown below , You can also add your favorite patterns 、 Words etc. .
2.2 Python Environment and modules
One is installed Python Computers in the environment ,Python The environment requires the following modules to be installed .
① 200 Multiple copies Python e-book ( And classic books ) Should have
② Python Standard library information ( The most complete Chinese version )
③ Project source code ( Forty or fifty interesting and reliable hand training projects and source code )
④ Python Basic introduction 、 Reptiles 、 Network development 、 Big data analysis video ( Suitable for Xiaobai to learn )
⑤ Python Learning Roadmap ( Farewell to bad learning )
Python Exchange of learning Q Group 101677771 Oh
numpy pillow wxgl
Without the above modules , Please refer to the following command to install .
pip install numpy pip install pillow pip install wxgl
3. Source code
# The Lantern Festival is coming , Would you like a lantern ?3D That kind of # -*- 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= Lantern height cols= The circumference of the bottom circle of the Lantern r, h = 1, 2*np.pi*rows/cols # r=1.5 h=2.66 # Make radius 1 A unit of 、 Height 2.668 A unit of cylindrical keel . # take 0 To 2π Divide into 942 Share theta = np.linspace(0, 2*np.pi, cols) x = r*np.cos(theta) y = r*np.sin(theta) # take 0 To h=2.66 Divide into 400 Share 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 # Make a rotating impeller theta = np.linspace(0, 2*np.pi, 18, endpoint=False) x = r*np.cos(theta) y = r*np.sin(theta) 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) # every other 10 Point extraction 1 A little bit , use mesh To draw the shape of the keel 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]) # The rotating impeller is painted red 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) # White light 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') # Red line lantern line 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__) # Red title plt.title(text=' Happy Lantern Festival !', size=64, color='red', pos=(0,0,0),align='center',weight='bold') plt.show(rotation='h-')
4、 effect