-2

I've got a task of reading a stl image and need to display in my python environment. To do that, I have written the code below, but it is not working. When I tried with plt.imshow instead of plt.show(), it shows only vertical column with shape (92520, 3, 3). Is it possible to view the full 3D image I have or not?

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from stl import mesh

# Load the STL mesh
your_mesh = mesh.Mesh.from_file('/content/stlimage.stl')

# Create a new figure
fig = plt.figure()
ax = mplot3d.Axes3D(fig)

# Add the mesh to the plot
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Set plot labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Show the plot
plt.show()
jared
  • 4,165
  • 1
  • 8
  • 31
  • Please explain how the tag [tag:STL], a C++ library is related to Python, the file extension or 3d images? – 273K Aug 26 '23 at 17:07
  • @273K because the OP didn't read the tag description. – Trenton McKinney Aug 26 '23 at 17:19
  • It's not reproducible without the file. Also does it need to be scaled? [How to plot a .stl file with matplotlib](https://stackoverflow.com/q/74617585/7758804) and [How to zoom_in 3D plot](https://stackoverflow.com/q/65016029/7758804) – Trenton McKinney Aug 26 '23 at 17:28

1 Answers1

2

You can use the open3d package to 3D visualize a .stl file. Here's how you can do it:

  • Installation
pip install open3d
  • Usage
import open3d as o3d

mesh = o3d.io.read_triangle_mesh("DeathStarTop_Hollow.stl")
mesh = mesh.compute_vertex_normals()
o3d.visualization.draw_geometries([mesh], window_name="STL", left=1000, top=200, width=800, height=650)

Output: enter image description here

You will see an interactable 3D stl file.

Check out open3d documentation to know more.

Musabbir Arrafi
  • 744
  • 4
  • 18