Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Lantern Festival Walking Lantern with Python 3D Technology

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python 3D technology to make Lantern Festival Walking Lantern". The content of the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn how to use Python 3D technology to make Lantern Festival Walking Lantern Lantern.

Effect picture:

Raw material 2.1 Lantern Paper

You can add your favorite patterns, words, etc.

2.2 Python environment and module

A computer with Python environment installed, Python environment needs to install the following modules.

Numpy

Pillow

Wxgl

If you do not have the above module, please refer to the following command to install.

Pip install numpypip install pillowpip install wxgl

NumPy and pillow are the most commonly used scientific computing libraries and image processing libraries under Python, and they are commonly used modules. WxGL is a three-dimensional data visualization library based on PyOpenGL, which uses wx as the display back end and provides Matplotlib-style interactive application mode. at the same time, it can also be seamlessly combined with wxPython to draw three-dimensional models on the form of wx.

Third, the production process

The lantern production process is very simple, with only 30 lines of code, and can be executed line by line interactively in Python IDLE.

3.1Import module > import numpy as np > from PIL import Image > import wxgl.wxplot as plt3.2 Open Lantern Paper Image > fn = rusted:\ temp\ light0115\ res\ paper.png' > im = np.array (Image.open (fn)) / 255 > im.shape (400,942,3)

Fn defines the image storage path, please modify it according to the reality. Image.open (fn) opens the file and returns a PIL object, and np.array () converts the PIL object into a numpy.ndarray array object. Divide by 255. change the range of image data from 0 to 255 to 0 to 1 to meet the interface requirements of WxGL. Looking at the shape of the array, the image is displayed at a resolution of 400 pixels high and 942 pixels wide, with three colors for each pixel (in this case, RGB).

3.3 make keel according to the size of lantern paper

The paper is 942 pixels long and rolled into a cylinder with a radius of 149.9 pixels. If the radius is regarded as 1 unit, the height of 400 pixels is equivalent to 2.668 units.

> > rows, cols, deep = im.shape > cols/ (2*np.pi) 149.9239563925654 > r = 1 > > h = 2*np.pi*rows/cols > h2.6680192387174464

Next, you need to make a cylindrical keel with a radius of 1 unit and a height of 2.668 units.

> theta = np.linspace (0, 2*np.pi, cols) > > x = r * np.cos (theta) > y = r * np.sin (theta) > > 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))

The xs, ys and zs here are the x, y and z coordinates of each point on the cylindrical keel. The following code, every 10 points to extract a point, using mesh method to draw the keel shape. Of course, you can also draw all the points so that the vertices will be linked together.

> plt.mesh (xs [:: 10 dagger ys 10], zs [:: 10 dagger 10], mode='FLBL') > plt.show () 3.4 paste the keel with lantern paper

With the keel, then you can stick the lantern paper on the keel. Before continuing, remember to close the 3D keel window that just popped up.

> plt.mesh (xs, ys, zs, im) > plt.show ()

However, you will immediately find that the lantern paper is pasted upside down. It doesn't matter. We can reverse the direction like this.

> plt.mesh (xs, ys, zs, im [::-1]) > plt.show () 3.5 to make rotating impeller

The reason why the walking lantern can rotate is that the candle inside is heated to form an updraft, which drives the top impeller to rotate, thus driving the lantern to rotate. Of course, the impeller here is just a shape, and the rotation of the lantern depends on another mechanism.

> > theta = np.linspace (0, 2*np.pi, 18, endpoint=False) > > x = r * np.cos (theta) > y = r * np.sin (theta) > x [2endpoint=False 3] = x [12*np.pi 3] = 0 > y [1RV 3] = y [1RV 3] > > y [1RV 3] = 0 > z = np.ones (18) * h * 0.9 > vs = np.stack (xs, ys) > > plt.mesh Zs, im [::-1]) > > plt.surface (vs, color='#C03000', method='T', mode='FCBC', alpha=0.8) > plt.show ()

Impeller design has 6 pieces, simulated with triangle, the color is dark red, transparency is 0.8, the overall effect is a little rough.

3.6 plus lighting and lifting system

The lamp is represented by a white ball, while the cable is a straight line in red, which also serves as the power cord for the lamp.

> plt.mesh (xs, ys, zs, im [::-1]) > plt.surface (vs, color='#C03000', method='T', mode='FCBC', alpha=0.8) > plt.sphere ((0 FFFFFF', slices=60 0.4),'# FFFFFF', slices=60, mode='FCBC') > plt.plot ((0 Ling 0), (0 Ling 0), (0 J 0), (0 4 h, 1.5 h), width=3.0, style='solid', cmap='hsv' (caxis='z') 3.7Let the lantern turn

The implementation of lantern rotation is very simple, you only need to give the show method a rotation parameter.

Plt.show (rotation='h-') IV. Complete source code

With the above explanation, there is no need to comment on the complete source code. More than 30 lines of code, you can expand yourself to make more lanterns.

#-*-coding: utf-8-*-import numpy as npfrom PIL import Imageimport wxgl.wxplot as pltim = np.array (Image.open ('res/paper.png')) / 255rows, cols, deep = im.shaper, h = 1, 2*np.pi*rows/colstheta = np.linspace (0, 2*np.pi, cols) x = r*np.cos (theta) y = r*np.sin (theta) 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)) theta = np.linspace (0, 2*np.pi, 18 Endpoint=False) x = r*np.cos (theta) y = r*np.sin (theta) x [2r*np.sin (theta)] = x [1:: 3] = 0y [2r*np.sin 3] = y [1:: 3] y [1:: 3] = 0z = np.ones (18) * h * 0.9vs = np.stack ((xMagneyMagnez), axis=1) plt.mesh (xs, ys, zs, im [::-1]) plt.surface (vs, color='#C03000') Method='T', mode='FCBC', alpha=0.8) plt.sphere ((0 FFFFFF', slices=60, mode='FCBC') plt.plot ((0 FFFFFF', slices=60, 0), (0), (0), (0), (0. 4H, 1.5), width=3.0, style='solid', cmap='hsv', caxis='z') plt.show (rotation='h-') Thank you for your reading The above is the content of "how to use Python 3D technology to make Lantern Festival Walking Lantern Lantern". After the study of this article, I believe you have a deeper understanding of how to use Python 3D technology to make Lantern Festival Walking Lantern. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report