| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import os
- import matplotlib
- matplotlib.use('Agg')
- import matplotlib.pyplot as plt
- from stl import mesh
- from mpl_toolkits import mplot3d
- def generate_stl_preview(stl_path: str, output_path: str):
- """
- Generates a PNG preview image for an STL file.
- """
- try:
- # Load the STL file
- your_mesh = mesh.Mesh.from_file(stl_path)
- # Create a new plot
- figure = plt.figure(figsize=(8, 8))
- # Use transparent background for a premium look
- figure.patch.set_alpha(0.0)
-
- axes = figure.add_subplot(111, projection='3d')
- axes.set_facecolor((0,0,0,0)) # Transparent background inside plot
- # Add the mesh to the plot
- poly = mplot3d.art3d.Poly3DCollection(your_mesh.vectors)
- # Professional-looking blue/gray color
- poly.set_facecolor([0.2, 0.5, 0.8, 0.9])
- poly.set_edgecolor([0.1, 0.1, 0.1, 0.2])
- axes.add_collection3d(poly)
- # Auto-scale the plot
- scale = your_mesh.points.flatten()
- axes.auto_scale_xyz(scale, scale, scale)
-
- # Hide axes for a clean look
- axes.set_axis_off()
-
- # Adjust view for better perspective
- axes.view_init(elev=30, azim=45)
- # Save the result
- plt.savefig(output_path, dpi=100, bbox_inches='tight', transparent=True)
- plt.close(figure)
- return True
- except Exception as e:
- print(f"Error generating preview: {e}")
- return False
|