import pymesh
from PIL import Image

# Load the image file
img = Image.open("heightfield.png")

# Get the dimensions of the image
width, height = img.size

# Create a mesh using PyMesh
mesh = pymesh.form_mesh(np.zeros((width * height, 3)), np.zeros((0, 3)))

# Set the vertex positions of the mesh based on the image height values
for i in range(width):
    for j in range(height):
        index = i + j * width
        vertex = [i, j, img.getpixel((i, j))[0]]
        mesh.vertices[index] = vertex

# Generate the surface mesh using PyMesh
mesh = pymesh.compute_outer_hull(mesh)

# Save the mesh as an STL file
pymesh.save_mesh("surface.stl", mesh)