File size: 3,162 Bytes
98f6ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import numpy as np
from PIL import Image
from stl import mesh
import plotly.graph_objects as go
from flask import Flask, render_template_string, request, send_file
from werkzeug.utils import secure_filename

app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'outputs'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER

HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>3D Model Generator for 3D Printing</title>
</head>
<body>
    <h1>Upload Image to Generate 3D Printable Model</h1>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <label>Image:</label><input type="file" name="image" required><br><br>
        <input type="submit" value="Upload & Generate 3D Model">
    </form>
    {% if plot_div %}
    <h2>3D Model Preview:</h2>
    {{ plot_div|safe }}
    <br>
    <a href="{{ download_link }}" download>Download STL File</a>
    {% endif %}
</body>
</html>
"""

def generate_3d_model(image_path, output_stl_path, scale=1.0, height_scale=5.0):
    image = Image.open(image_path).convert('L')
    img_array = np.array(image)
    
    height, width = img_array.shape
    x, y = np.meshgrid(np.linspace(0, width * scale, width), np.linspace(0, height * scale, height))
    z = (img_array / 255.0) * height_scale

    # Create vertices and faces
    vertices = np.column_stack((x.flatten(), y.flatten(), z.flatten()))
    faces = []
    for i in range(height - 1):
        for j in range(width - 1):
            idx = i * width + j
            faces.append([idx, idx + 1, idx + width])
            faces.append([idx + 1, idx + width + 1, idx + width])

    # Create STL mesh
    model_mesh = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
    for i, f in enumerate(faces):
        for j in range(3):
            model_mesh.vectors[i][j] = vertices[f[j], :]
    
    model_mesh.save(output_stl_path)
    
    fig = go.Figure(data=[go.Surface(z=z, x=x, y=y, colorscale='Gray')])
    fig.update_layout(scene=dict(aspectratio=dict(x=1, y=1, z=0.3)), margin=dict(l=0, r=0, b=0, t=0))
    
    return fig.to_html(full_html=False)

@app.route('/', methods=['GET'])
def index():
    return render_template_string(HTML_TEMPLATE, plot_div=None, download_link=None)

@app.route('/upload', methods=['POST'])
def upload():
    image = request.files['image']
    image_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(image.filename))
    image.save(image_path)

    stl_filename = os.path.splitext(image.filename)[0] + '.stl'
    output_stl_path = os.path.join(app.config['OUTPUT_FOLDER'], stl_filename)

    plot_div = generate_3d_model(image_path, output_stl_path)
    download_link = f"/download/{stl_filename}"

    return render_template_string(HTML_TEMPLATE, plot_div=plot_div, download_link=download_link)

@app.route('/download/<filename>')
def download_file(filename):
    return send_file(os.path.join(app.config['OUTPUT_FOLDER'], filename), as_attachment=True)

if __name__ == '__main__':
    app.run(debug=True)