TruthLens commited on
Commit
98f6ff1
·
verified ·
1 Parent(s): 31c7898

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ from PIL import Image
4
+ from stl import mesh
5
+ import plotly.graph_objects as go
6
+ from flask import Flask, render_template_string, request, send_file
7
+ from werkzeug.utils import secure_filename
8
+
9
+ app = Flask(__name__)
10
+ UPLOAD_FOLDER = 'uploads'
11
+ OUTPUT_FOLDER = 'outputs'
12
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
13
+ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
14
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
15
+ app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
16
+
17
+ HTML_TEMPLATE = """
18
+ <!DOCTYPE html>
19
+ <html lang="en">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>3D Model Generator for 3D Printing</title>
23
+ </head>
24
+ <body>
25
+ <h1>Upload Image to Generate 3D Printable Model</h1>
26
+ <form action="/upload" method="post" enctype="multipart/form-data">
27
+ <label>Image:</label><input type="file" name="image" required><br><br>
28
+ <input type="submit" value="Upload & Generate 3D Model">
29
+ </form>
30
+ {% if plot_div %}
31
+ <h2>3D Model Preview:</h2>
32
+ {{ plot_div|safe }}
33
+ <br>
34
+ <a href="{{ download_link }}" download>Download STL File</a>
35
+ {% endif %}
36
+ </body>
37
+ </html>
38
+ """
39
+
40
+ def generate_3d_model(image_path, output_stl_path, scale=1.0, height_scale=5.0):
41
+ image = Image.open(image_path).convert('L')
42
+ img_array = np.array(image)
43
+
44
+ height, width = img_array.shape
45
+ x, y = np.meshgrid(np.linspace(0, width * scale, width), np.linspace(0, height * scale, height))
46
+ z = (img_array / 255.0) * height_scale
47
+
48
+ # Create vertices and faces
49
+ vertices = np.column_stack((x.flatten(), y.flatten(), z.flatten()))
50
+ faces = []
51
+ for i in range(height - 1):
52
+ for j in range(width - 1):
53
+ idx = i * width + j
54
+ faces.append([idx, idx + 1, idx + width])
55
+ faces.append([idx + 1, idx + width + 1, idx + width])
56
+
57
+ # Create STL mesh
58
+ model_mesh = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
59
+ for i, f in enumerate(faces):
60
+ for j in range(3):
61
+ model_mesh.vectors[i][j] = vertices[f[j], :]
62
+
63
+ model_mesh.save(output_stl_path)
64
+
65
+ fig = go.Figure(data=[go.Surface(z=z, x=x, y=y, colorscale='Gray')])
66
+ fig.update_layout(scene=dict(aspectratio=dict(x=1, y=1, z=0.3)), margin=dict(l=0, r=0, b=0, t=0))
67
+
68
+ return fig.to_html(full_html=False)
69
+
70
+ @app.route('/', methods=['GET'])
71
+ def index():
72
+ return render_template_string(HTML_TEMPLATE, plot_div=None, download_link=None)
73
+
74
+ @app.route('/upload', methods=['POST'])
75
+ def upload():
76
+ image = request.files['image']
77
+ image_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(image.filename))
78
+ image.save(image_path)
79
+
80
+ stl_filename = os.path.splitext(image.filename)[0] + '.stl'
81
+ output_stl_path = os.path.join(app.config['OUTPUT_FOLDER'], stl_filename)
82
+
83
+ plot_div = generate_3d_model(image_path, output_stl_path)
84
+ download_link = f"/download/{stl_filename}"
85
+
86
+ return render_template_string(HTML_TEMPLATE, plot_div=plot_div, download_link=download_link)
87
+
88
+ @app.route('/download/<filename>')
89
+ def download_file(filename):
90
+ return send_file(os.path.join(app.config['OUTPUT_FOLDER'], filename), as_attachment=True)
91
+
92
+ if __name__ == '__main__':
93
+ app.run(debug=True)