将3D .stl文件转换为JPG图像

发布于 2025-01-21 00:10:13 字数 790 浏览 3 评论 0原文

如何将任何 STL 文件中的 3D 对象转换为 JPG 或 PNG 图像。

我尝试在网上进行一些搜索,但无法找到任何可能的解决方案。

谁能帮助我编写可以使用 Python 完成这项直接任务的代码?有什么图书馆可以帮助解决这个问题吗?

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

DIR = str(pathlib.Path(__file__).parent.resolve()).replace('\\', '/')
path = f'{DIR}/any_stl_file.stl'

# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file(path)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

pyplot.savefig(f"{DIR}/the_image.jpg")

How do I convert a 3D object in any STL file into a JPG or PNG image.

I tried searching a little bit online but I wasn't been able to arrive at finding any possible solutions.

Can anyone help me with the code that can do this straight forward task with Python? IS there any libraries that can help with that?

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

DIR = str(pathlib.Path(__file__).parent.resolve()).replace('\\', '/')
path = f'{DIR}/any_stl_file.stl'

# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file(path)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

pyplot.savefig(f"{DIR}/the_image.jpg")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜`诱少女 2025-01-28 00:10:13

看看https://pypi.org/project/numpy-stl/

这个代码片段来自上面的链接。

python 3.12.0ma​​tplotlib 3.8.0stl 3.0.1中测试

从 stl 导入网格
从 mpl_toolkits 导入 mplot3d
从 matplotlib 导入 pyplot

# 创建一个新图
图 = pyplot.figure()
轴=图.add_subplot(投影='3d')

# 加载 STL 文件并将向量添加到绘图中 
# 文件位于 https://github.com/wolph/numpy-stl/tree/develop/tests/stl_binary
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# 自动缩放到网格尺寸
比例= your_mesh.points.flatten()
轴.auto_scale_xyz(比例尺,比例尺,比例尺)

# 将绘图显示到屏幕上
pyplot.show()

要在 pyplot.show() 之前将 pyplot 对象保存为图像:

pyplot.savefig("file_name.jpg")

在此处输入图像描述

Take a look at https://pypi.org/project/numpy-stl/

This code snippet is from the link above.

Tested in python 3.12.0, matplotlib 3.8.0, stl 3.0.1

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

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

# Load the STL files and add the vectors to the plot 
# file is at https://github.com/wolph/numpy-stl/tree/develop/tests/stl_binary
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

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

To save a pyplot object as an image before pyplot.show():

pyplot.savefig("file_name.jpg")

enter image description here

若水般的淡然安静女子 2025-01-28 00:10:13

正如 numpy-stl 中的示例所述,您必须使用 pyplot 的 savefig:

pyplot.savefig('model.jpg', format='jpg', bbox_inches='tight')

默认为巴布亚新几内亚。我必须添加 bbox_inches 以避免出现空图像。查看 文档 了解更多选项。这是完整的工作示例,改编自链接资源:

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

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

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('model.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

# Save as image
pyplot.savefig('model.jpg', format='jpg', bbox_inches='tight')

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

As mentioned in the example from numpy-stl you have to use pyplot's savefig:

pyplot.savefig('model.jpg', format='jpg', bbox_inches='tight')

The default is PNG. I had to add bbox_inches to avoid an empty image. Check out the docs for more options. Here is the full working example, adapted from the linked resource:

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

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

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('model.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

# Save as image
pyplot.savefig('model.jpg', format='jpg', bbox_inches='tight')

# Show the plot to the screen
pyplot.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文