在Python中从STL文件渲染2D图像 [英] Rendering 2D images from STL file in Python

查看:948
本文介绍了在Python中从STL文件渲染2D图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加载一个STL文件并以不同的旋转方式生成一组2D图像。



我了解了基于numpy-stl的基础知识,该基于



我试图突出显示边缘。但这对模型的创建方式很敏感,这对我来说并不好。





我希望通过添加照明,阴影可以帮助添加丢失的细节,但我找不到解决方法。



有什么想法如何向下面的代码添加光源以创建阴影?

解决方案

在对Mayavi的安装灾难感到厌倦之后,我最终为此编写了自己的库。
https://github.com/bwoodsend/vtkplotlib



您的代码将类似于

 从stl.mesh导入vtkplotlib作为vpl 
import Mesh

path =您的路径here.stl

#使用numpy-stl
mesh = Mesh.from_file(path)$ b $读取STL b
#绘制网格
vpl.mesh_plot(网格)

#显示图形
vpl.show()

如果您希望砖为蓝色,则可以用

vpl.mesh_plot(mesh,color = blue)


I would like to load an STL file and produce a set of 2D images in different rotations.

I got the basics working with numpy-stl based on this example, ended up with this code -

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot

filename = '3001.stl'

# Create a new plot
figure = pyplot.figure()
axes = figure.gca(projection='3d')

# Load the STL files and add the vectors to the plot
mesh = mesh.Mesh.from_file(filename)

axes.add_collection3d(mplot3d.art3d.Poly3DCollection(mesh.vectors, color='lightgrey'))
#axes.plot_surface(mesh.x,mesh.y,mesh.z)
# Auto scale to the mesh size
scale = mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

#turn off grid and axis from display      
pyplot.axis('off')

#set viewing angle
axes.view_init(azim=120)

# Show the plot to the screen
pyplot.show()

This works well only that I end up with a silhouette of the component, lacking a lot of the detail. the picture below is a lego brick...

I tried to highlight the edges. but that is sensitive to how the model was created, which is not great for me.

I was hoping that by adding lighting, the shadows could help add the missing detail but I can't find a way to do that.

Any idea how to add lightsource to the code below to create shadows ?

解决方案

After getting tired with Mayavi's install disasters I ended up writing my own library for this. https://github.com/bwoodsend/vtkplotlib

Your code would be something like

import vtkplotlib as vpl
from stl.mesh import Mesh

path = "your path here.stl"

# Read the STL using numpy-stl
mesh = Mesh.from_file(path)

# Plot the mesh
vpl.mesh_plot(mesh)

# Show the figure
vpl.show()

If you want the brick to be blue you can replace the mesh_plot with

vpl.mesh_plot(mesh, color="blue")

这篇关于在Python中从STL文件渲染2D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆