wolph / numpy-stl

Simple library to make working with STL files (and 3D objects in general) fast and easy.
http://numpy-stl.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
624 stars 105 forks source link

Convert to jpg file #59

Closed burdeinyi closed 7 years ago

burdeinyi commented 7 years ago

Sorry for a silly question. Is here a way to take something like a snapshot of stl file in jpeg format? If it is not, may somebody know how to do this stuff? Thanks.

Uvar commented 7 years ago

That would involve specifying your viewpoint (origin and direction), subsequently squashing your mesh faces which are visible (Raytracing?) into a 2D view, then rendering the view. I think the Cura source code might prove to be a valuable source of information on how to achieve this.

wolph commented 7 years ago

It's actually fairly easy to do with matplotlib, the examples are almost exactly what you need except that instead of showing it on the screen you wish to write to a file:

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

# 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('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(-1)
axes.auto_scale_xyz(scale, scale, scale)

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

# Write the plot to a file
figure.savefig('mesh.png')

Note that this saves as a png instead of a jpg, for the images generated those will most likely be smaller and higher quality.