Spaces:
Running
Running
Upload 4 files
Browse files- README.md +22 -5
- app.py +394 -0
- relief_workshop.py +357 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,12 +1,29 @@
|
|
| 1 |
---
|
| 2 |
-
title: Pattern Ground
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: gray
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Pattern Ground — Relief Generator
|
| 3 |
+
emoji: 🏗️
|
| 4 |
colorFrom: gray
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "4.44.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Pattern Ground — Fabrication Topologies
|
| 13 |
+
|
| 14 |
+
Converti un'immagine in una tile di rilievo 3D stampabile.
|
| 15 |
+
|
| 16 |
+
Parte del progetto **Fabrication Topologies** — Vectorealism × Dropcity, Design Week Milano 2026.
|
| 17 |
+
|
| 18 |
+
## Come funziona
|
| 19 |
+
|
| 20 |
+
1. Carica un'immagine (foto di pavimentazione, mappa, pattern geometrico, texture…)
|
| 21 |
+
2. Regola i parametri di dimensione e rilievo
|
| 22 |
+
3. Controlla la depth map (bianco = alto, nero = basso)
|
| 23 |
+
4. Scarica l'STL e slicalo
|
| 24 |
+
|
| 25 |
+
## Parametri chiave
|
| 26 |
+
|
| 27 |
+
- **Quantize** — divide il rilievo in N livelli netti (effetto "stair", ottimo per pattern urbani)
|
| 28 |
+
- **Tileable** — sfuma i bordi così le tile si affiancano senza giunti
|
| 29 |
+
- **Invert** — in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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
app.py — Fabrication Topologies: Pattern Ground Relief Generator
|
| 3 |
+
Gradio web app per HuggingFace Spaces
|
| 4 |
+
|
| 5 |
+
Deploy:
|
| 6 |
+
1. Crea un nuovo Space su huggingface.co/new-space
|
| 7 |
+
2. Scegli SDK: Gradio | Hardware: CPU Basic (gratuito)
|
| 8 |
+
3. Carica questi 3 file: app.py, relief_workshop.py, requirements.txt
|
| 9 |
+
4. Il deploy parte automaticamente in ~2 minuti
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import io
|
| 13 |
+
import struct
|
| 14 |
+
import tempfile
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
|
| 17 |
+
import cv2
|
| 18 |
+
import gradio as gr
|
| 19 |
+
import numpy as np
|
| 20 |
+
from PIL import Image
|
| 21 |
+
|
| 22 |
+
# Importa le funzioni di processing da relief_workshop.py
|
| 23 |
+
from relief_workshop import build_heightmap, heightmap_to_stl
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# ─────────────────────────────────────────────
|
| 27 |
+
# CORE FUNCTION
|
| 28 |
+
# ─────────────────────────────────────────────
|
| 29 |
+
|
| 30 |
+
def generate_relief(
|
| 31 |
+
image,
|
| 32 |
+
tile_w: float,
|
| 33 |
+
tile_h_mode: str,
|
| 34 |
+
tile_h_custom: float,
|
| 35 |
+
relief_mm: float,
|
| 36 |
+
base_mm: float,
|
| 37 |
+
invert: bool,
|
| 38 |
+
quantize_levels: int,
|
| 39 |
+
tileable: bool,
|
| 40 |
+
blur: float,
|
| 41 |
+
resolution: int,
|
| 42 |
+
):
|
| 43 |
+
if image is None:
|
| 44 |
+
return None, None, "⚠️ Carica un'immagine per iniziare."
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# Salva immagine in temp
|
| 48 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
| 49 |
+
tmp_in = f.name
|
| 50 |
+
Image.fromarray(image).save(tmp_in)
|
| 51 |
+
|
| 52 |
+
# Calcola altezza tile
|
| 53 |
+
img_h, img_w = image.shape[:2]
|
| 54 |
+
if tile_h_mode == "Stessa larghezza (100×100)":
|
| 55 |
+
tile_h = tile_w
|
| 56 |
+
elif tile_h_mode == "Proporzioni originali":
|
| 57 |
+
tile_h = tile_w * img_h / img_w
|
| 58 |
+
else:
|
| 59 |
+
tile_h = tile_h_custom
|
| 60 |
+
|
| 61 |
+
# Build heightmap
|
| 62 |
+
quantize = quantize_levels if quantize_levels >= 2 else 0
|
| 63 |
+
height_map, px_w, px_h = build_heightmap(
|
| 64 |
+
tmp_in,
|
| 65 |
+
max_px=resolution,
|
| 66 |
+
invert=invert,
|
| 67 |
+
blur=blur,
|
| 68 |
+
gamma=1.1,
|
| 69 |
+
quantize=quantize,
|
| 70 |
+
tileable=tileable,
|
| 71 |
+
fade_px=12,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Preview depth map
|
| 75 |
+
preview = (height_map * 255).astype(np.uint8)
|
| 76 |
+
preview_rgb = np.stack([preview, preview, preview], axis=-1)
|
| 77 |
+
|
| 78 |
+
# Generate STL
|
| 79 |
+
stl_bytes = heightmap_to_stl(
|
| 80 |
+
height_map, px_w, px_h,
|
| 81 |
+
tile_w_mm=tile_w,
|
| 82 |
+
tile_h_mm=tile_h,
|
| 83 |
+
relief_mm=relief_mm,
|
| 84 |
+
base_mm=base_mm,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Save STL to temp file
|
| 88 |
+
with tempfile.NamedTemporaryFile(suffix=".stl", delete=False) as f:
|
| 89 |
+
stl_path = f.name
|
| 90 |
+
f.write(stl_bytes)
|
| 91 |
+
|
| 92 |
+
size_kb = len(stl_bytes) / 1024
|
| 93 |
+
n_tri = (len(stl_bytes) - 84) // 50
|
| 94 |
+
|
| 95 |
+
info = f"""✓ Tile generata
|
| 96 |
+
|
| 97 |
+
Dimensioni: {tile_w:.0f} × {tile_h:.0f} mm
|
| 98 |
+
Altezza: base {base_mm:.1f} mm + rilievo {relief_mm:.1f} mm = {base_mm + relief_mm:.1f} mm totale
|
| 99 |
+
Mesh: {px_w} × {px_h} px — {n_tri:,} triangoli
|
| 100 |
+
File STL: {size_kb:.0f} KB
|
| 101 |
+
|
| 102 |
+
Impostazioni slicer: layer 0.15 mm · infill 15% · no supports"""
|
| 103 |
+
|
| 104 |
+
Path(tmp_in).unlink(missing_ok=True)
|
| 105 |
+
return preview_rgb, stl_path, info
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return None, None, f"❌ Errore: {str(e)}"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def preview_only(
|
| 112 |
+
image,
|
| 113 |
+
tile_w, tile_h_mode, tile_h_custom,
|
| 114 |
+
relief_mm, base_mm,
|
| 115 |
+
invert, quantize_levels, tileable, blur, resolution,
|
| 116 |
+
):
|
| 117 |
+
"""Genera solo la depth map preview, più veloce."""
|
| 118 |
+
if image is None:
|
| 119 |
+
return None, "⚠️ Carica un'immagine per iniziare."
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
|
| 123 |
+
tmp_in = f.name
|
| 124 |
+
Image.fromarray(image).save(tmp_in)
|
| 125 |
+
|
| 126 |
+
quantize = quantize_levels if quantize_levels >= 2 else 0
|
| 127 |
+
height_map, px_w, px_h = build_heightmap(
|
| 128 |
+
tmp_in,
|
| 129 |
+
max_px=min(resolution, 300), # preview più veloce
|
| 130 |
+
invert=invert,
|
| 131 |
+
blur=blur,
|
| 132 |
+
gamma=1.1,
|
| 133 |
+
quantize=quantize,
|
| 134 |
+
tileable=tileable,
|
| 135 |
+
fade_px=12,
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
preview = (height_map * 255).astype(np.uint8)
|
| 139 |
+
preview_rgb = np.stack([preview, preview, preview], axis=-1)
|
| 140 |
+
|
| 141 |
+
Path(tmp_in).unlink(missing_ok=True)
|
| 142 |
+
|
| 143 |
+
msg = f"Depth map: {px_w}×{px_h}px · Bianco = alto · Nero = basso"
|
| 144 |
+
if invert:
|
| 145 |
+
msg += " (invertita)"
|
| 146 |
+
return preview_rgb, msg
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
return None, f"❌ Errore: {str(e)}"
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# ─────────────────────────────────────────────
|
| 153 |
+
# UI
|
| 154 |
+
# ─────────────────────────────────────────────
|
| 155 |
+
|
| 156 |
+
CSS = """
|
| 157 |
+
@import url('https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Instrument+Serif:ital@0;1&display=swap');
|
| 158 |
+
|
| 159 |
+
:root {
|
| 160 |
+
--black: #0a0a0a;
|
| 161 |
+
--white: #f2efe8;
|
| 162 |
+
--yellow: #d4a912;
|
| 163 |
+
--grey: #333;
|
| 164 |
+
--mid: #888;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
body, .gradio-container {
|
| 168 |
+
background: var(--black) !important;
|
| 169 |
+
color: var(--white) !important;
|
| 170 |
+
font-family: 'DM Mono', monospace !important;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.gr-block, .gr-box, .gr-panel {
|
| 174 |
+
background: #111 !important;
|
| 175 |
+
border: 1px solid #222 !important;
|
| 176 |
+
border-radius: 0 !important;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.gr-button {
|
| 180 |
+
border-radius: 0 !important;
|
| 181 |
+
font-family: 'DM Mono', monospace !important;
|
| 182 |
+
letter-spacing: 0.05em !important;
|
| 183 |
+
text-transform: uppercase !important;
|
| 184 |
+
font-size: 0.75rem !important;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.gr-button.primary {
|
| 188 |
+
background: var(--yellow) !important;
|
| 189 |
+
color: var(--black) !important;
|
| 190 |
+
border: none !important;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
.gr-button.secondary {
|
| 194 |
+
background: transparent !important;
|
| 195 |
+
color: var(--mid) !important;
|
| 196 |
+
border: 1px solid #333 !important;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.gr-button:hover {
|
| 200 |
+
opacity: 0.85 !important;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
label, .gr-label {
|
| 204 |
+
font-family: 'DM Mono', monospace !important;
|
| 205 |
+
font-size: 0.7rem !important;
|
| 206 |
+
letter-spacing: 0.08em !important;
|
| 207 |
+
color: var(--mid) !important;
|
| 208 |
+
text-transform: uppercase !important;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
.gr-textbox textarea, .gr-textbox input {
|
| 212 |
+
background: #0d0d0d !important;
|
| 213 |
+
border: 1px solid #222 !important;
|
| 214 |
+
color: var(--white) !important;
|
| 215 |
+
font-family: 'DM Mono', monospace !important;
|
| 216 |
+
border-radius: 0 !important;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
h1 {
|
| 220 |
+
font-family: 'Instrument Serif', serif !important;
|
| 221 |
+
font-size: 2.2rem !important;
|
| 222 |
+
color: var(--white) !important;
|
| 223 |
+
font-weight: 400 !important;
|
| 224 |
+
letter-spacing: -0.02em !important;
|
| 225 |
+
line-height: 1.1 !important;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
h3 {
|
| 229 |
+
font-family: 'DM Mono', monospace !important;
|
| 230 |
+
font-size: 0.65rem !important;
|
| 231 |
+
letter-spacing: 0.15em !important;
|
| 232 |
+
color: var(--yellow) !important;
|
| 233 |
+
text-transform: uppercase !important;
|
| 234 |
+
margin-bottom: 0.5rem !important;
|
| 235 |
+
border-bottom: 1px solid #222 !important;
|
| 236 |
+
padding-bottom: 0.4rem !important;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
.info-box {
|
| 240 |
+
background: #111;
|
| 241 |
+
border: 1px solid #222;
|
| 242 |
+
padding: 1rem;
|
| 243 |
+
font-size: 0.75rem;
|
| 244 |
+
line-height: 1.7;
|
| 245 |
+
color: var(--mid);
|
| 246 |
+
white-space: pre-line;
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
.accent { color: var(--yellow); }
|
| 250 |
+
|
| 251 |
+
/* Slider */
|
| 252 |
+
.gr-slider input[type=range] {
|
| 253 |
+
accent-color: var(--yellow) !important;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
/* Checkbox */
|
| 257 |
+
input[type=checkbox] {
|
| 258 |
+
accent-color: var(--yellow) !important;
|
| 259 |
+
}
|
| 260 |
+
"""
|
| 261 |
+
|
| 262 |
+
HEADER = """
|
| 263 |
+
# Pattern Ground
|
| 264 |
+
### Fabrication Topologies — Vectorealism × Dropcity
|
| 265 |
+
|
| 266 |
+
Converti un'immagine in una tile di rilievo 3D pronta per la stampa.
|
| 267 |
+
Regola i parametri, controlla la depth map, scarica l'STL.
|
| 268 |
+
"""
|
| 269 |
+
|
| 270 |
+
INSTRUCTIONS = """### Come funziona
|
| 271 |
+
|
| 272 |
+
**Depth map preview** — bianco = alto, nero = basso. Controlla prima di generare l'STL.
|
| 273 |
+
|
| 274 |
+
**Quantize** — crea gradini netti invece di un rilievo continuo. 4–6 livelli funziona bene per pattern urbani (sanpietrini, griglia, muratura).
|
| 275 |
+
|
| 276 |
+
**Tileable** — sfuma i bordi della tile così le piastrelle si affiancano senza giunti visibili nella griglia condivisa.
|
| 277 |
+
|
| 278 |
+
**Invert** — inverte l'altezza: utile se il pattern ha sfondo bianco e motivo scuro.
|
| 279 |
+
|
| 280 |
+
**Impostazioni slicer consigliate:** layer height 0.15 mm · infill 15% · no supports · ironing on top surface"""
|
| 281 |
+
|
| 282 |
+
def build_ui():
|
| 283 |
+
with gr.Blocks(css=CSS, title="Pattern Ground — Fabrication Topologies") as demo:
|
| 284 |
+
|
| 285 |
+
gr.Markdown(HEADER)
|
| 286 |
+
|
| 287 |
+
with gr.Row():
|
| 288 |
+
|
| 289 |
+
# ── Colonna sinistra: input + parametri ──
|
| 290 |
+
with gr.Column(scale=1):
|
| 291 |
+
|
| 292 |
+
gr.Markdown("### Immagine")
|
| 293 |
+
image_input = gr.Image(
|
| 294 |
+
label="Carica immagine (PNG, JPG)",
|
| 295 |
+
type="numpy",
|
| 296 |
+
image_mode="RGB",
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
gr.Markdown("### Dimensioni tile")
|
| 300 |
+
with gr.Row():
|
| 301 |
+
tile_w = gr.Slider(
|
| 302 |
+
20, 200, value=100, step=5,
|
| 303 |
+
label="Larghezza (mm)"
|
| 304 |
+
)
|
| 305 |
+
tile_h_mode = gr.Radio(
|
| 306 |
+
["Stessa larghezza (100×100)", "Proporzioni originali", "Personalizzata"],
|
| 307 |
+
value="Stessa larghezza (100×100)",
|
| 308 |
+
label="Altezza tile"
|
| 309 |
+
)
|
| 310 |
+
tile_h_custom = gr.Slider(
|
| 311 |
+
20, 200, value=100, step=5,
|
| 312 |
+
label="Altezza personalizzata (mm)",
|
| 313 |
+
visible=False
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
gr.Markdown("### Rilievo")
|
| 317 |
+
with gr.Row():
|
| 318 |
+
relief_mm = gr.Slider(1, 10, value=4, step=0.5, label="Altezza rilievo (mm)")
|
| 319 |
+
base_mm = gr.Slider(1, 5, value=2, step=0.5, label="Spessore base (mm)")
|
| 320 |
+
|
| 321 |
+
gr.Markdown("### Processing")
|
| 322 |
+
quantize_levels = gr.Slider(
|
| 323 |
+
0, 12, value=0, step=1,
|
| 324 |
+
label="Quantize — livelli di altezza (0 = continuo, 4–6 = gradini)"
|
| 325 |
+
)
|
| 326 |
+
blur = gr.Slider(
|
| 327 |
+
0, 3, value=0.5, step=0.1,
|
| 328 |
+
label="Smoothing (0 = nessuno)"
|
| 329 |
+
)
|
| 330 |
+
resolution = gr.Slider(
|
| 331 |
+
100, 400, value=200, step=50,
|
| 332 |
+
label="Risoluzione mesh (px per lato)"
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
gr.Markdown("### Opzioni")
|
| 336 |
+
with gr.Row():
|
| 337 |
+
invert = gr.Checkbox(label="Invert depth (scuro=alto)", value=False)
|
| 338 |
+
tileable = gr.Checkbox(label="Tileable (bordi sfumati)", value=False)
|
| 339 |
+
|
| 340 |
+
with gr.Row():
|
| 341 |
+
preview_btn = gr.Button("Depth map preview", variant="secondary")
|
| 342 |
+
generate_btn = gr.Button("Genera STL", variant="primary")
|
| 343 |
+
|
| 344 |
+
# ── Colonna destra: output ──
|
| 345 |
+
with gr.Column(scale=1):
|
| 346 |
+
gr.Markdown("### Depth map")
|
| 347 |
+
depth_preview = gr.Image(
|
| 348 |
+
label="Depth map — bianco = alto, nero = basso",
|
| 349 |
+
type="numpy",
|
| 350 |
+
interactive=False,
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
gr.Markdown("### Download")
|
| 354 |
+
stl_output = gr.File(label="File STL")
|
| 355 |
+
|
| 356 |
+
gr.Markdown("### Info")
|
| 357 |
+
info_output = gr.Textbox(
|
| 358 |
+
label="",
|
| 359 |
+
lines=8,
|
| 360 |
+
interactive=False,
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
gr.Markdown(INSTRUCTIONS)
|
| 364 |
+
|
| 365 |
+
# ── Logica UI ──
|
| 366 |
+
def toggle_custom_height(mode):
|
| 367 |
+
return gr.update(visible=(mode == "Personalizzata"))
|
| 368 |
+
|
| 369 |
+
tile_h_mode.change(toggle_custom_height, tile_h_mode, tile_h_custom)
|
| 370 |
+
|
| 371 |
+
# Inputs condivisi
|
| 372 |
+
inputs = [
|
| 373 |
+
image_input, tile_w, tile_h_mode, tile_h_custom,
|
| 374 |
+
relief_mm, base_mm, invert, quantize_levels, tileable, blur, resolution
|
| 375 |
+
]
|
| 376 |
+
|
| 377 |
+
preview_btn.click(
|
| 378 |
+
preview_only,
|
| 379 |
+
inputs=inputs,
|
| 380 |
+
outputs=[depth_preview, info_output],
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
generate_btn.click(
|
| 384 |
+
generate_relief,
|
| 385 |
+
inputs=inputs,
|
| 386 |
+
outputs=[depth_preview, stl_output, info_output],
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
return demo
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
if __name__ == "__main__":
|
| 393 |
+
app = build_ui()
|
| 394 |
+
app.launch()
|
relief_workshop.py
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
relief_workshop.py
|
| 3 |
+
Image → 3D relief tile — versione workshop Fabrication Topologies
|
| 4 |
+
|
| 5 |
+
Uso:
|
| 6 |
+
python relief_workshop.py input.png [opzioni]
|
| 7 |
+
|
| 8 |
+
Esempi:
|
| 9 |
+
python relief_workshop.py pattern.png
|
| 10 |
+
python relief_workshop.py pattern.png --width 100 --height 100 --relief 4 --base 2
|
| 11 |
+
python relief_workshop.py pattern.png --width 100 --height 100 --invert --tileable
|
| 12 |
+
python relief_workshop.py pattern.png --width 100 --height 100 --relief 6 --quantize 4
|
| 13 |
+
|
| 14 |
+
Dipendenze:
|
| 15 |
+
pip install numpy pillow scipy opencv-python-headless
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import argparse
|
| 19 |
+
import struct
|
| 20 |
+
import sys
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
|
| 23 |
+
import cv2
|
| 24 |
+
import numpy as np
|
| 25 |
+
from PIL import Image
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# ─────────────────────────────────────────────
|
| 29 |
+
# PROCESSING
|
| 30 |
+
# ─────────────────────────────────────────────
|
| 31 |
+
|
| 32 |
+
def load_and_prepare(image_path: str, max_px: int) -> np.ndarray:
|
| 33 |
+
"""Carica l'immagine, converte in grigio, ridimensiona."""
|
| 34 |
+
img = Image.open(image_path).convert("RGB")
|
| 35 |
+
ratio = min(max_px / img.width, max_px / img.height)
|
| 36 |
+
new_w = max(2, int(img.width * ratio))
|
| 37 |
+
new_h = max(2, int(img.height * ratio))
|
| 38 |
+
img = img.resize((new_w, new_h), Image.LANCZOS)
|
| 39 |
+
gray = np.array(img.convert("L"), dtype=np.float32)
|
| 40 |
+
return gray
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def smooth_heightmap(gray: np.ndarray, blur: float) -> np.ndarray:
|
| 44 |
+
"""Bilateral filter + smoothing leggero."""
|
| 45 |
+
if blur <= 0:
|
| 46 |
+
return gray
|
| 47 |
+
sigma_color = min(60, blur * 15)
|
| 48 |
+
sigma_space = min(10, blur * 3)
|
| 49 |
+
result = cv2.bilateralFilter(
|
| 50 |
+
gray.astype(np.uint8), d=9,
|
| 51 |
+
sigmaColor=sigma_color,
|
| 52 |
+
sigmaSpace=sigma_space
|
| 53 |
+
).astype(np.float32)
|
| 54 |
+
return result
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def quantize_heightmap(gray: np.ndarray, levels: int) -> np.ndarray:
|
| 58 |
+
"""Quantizza i livelli di altezza: crea un effetto a gradini netti."""
|
| 59 |
+
if levels < 2:
|
| 60 |
+
return gray
|
| 61 |
+
# Divide in N livelli e rimappa
|
| 62 |
+
normalized = gray / 255.0
|
| 63 |
+
quantized = np.floor(normalized * levels) / levels
|
| 64 |
+
return (quantized * 255.0).astype(np.float32)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def make_tileable(gray: np.ndarray, fade_px: int) -> np.ndarray:
|
| 68 |
+
"""
|
| 69 |
+
Sfuma i bordi verso la media dell'immagine per permettere l'affiancamento
|
| 70 |
+
senza scalini visibili ai giunti.
|
| 71 |
+
"""
|
| 72 |
+
if fade_px <= 0:
|
| 73 |
+
return gray
|
| 74 |
+
h, w = gray.shape
|
| 75 |
+
mean_val = float(np.mean(gray))
|
| 76 |
+
result = gray.copy()
|
| 77 |
+
|
| 78 |
+
fade = min(fade_px, w // 4, h // 4)
|
| 79 |
+
for i in range(fade):
|
| 80 |
+
alpha = i / fade # 0 al bordo → 1 all'interno
|
| 81 |
+
result[i, :] = alpha * gray[i, :] + (1 - alpha) * mean_val
|
| 82 |
+
result[h - 1 - i, :] = alpha * gray[h - 1 - i, :] + (1 - alpha) * mean_val
|
| 83 |
+
result[:, i] = alpha * result[:, i] + (1 - alpha) * mean_val
|
| 84 |
+
result[:, w - 1 - i] = alpha * result[:, w - 1 - i] + (1 - alpha) * mean_val
|
| 85 |
+
|
| 86 |
+
return result
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def build_heightmap(
|
| 90 |
+
image_path: str,
|
| 91 |
+
max_px: int = 500,
|
| 92 |
+
invert: bool = False,
|
| 93 |
+
blur: float = 0.5,
|
| 94 |
+
gamma: float = 1.1,
|
| 95 |
+
quantize: int = 0,
|
| 96 |
+
tileable: bool = False,
|
| 97 |
+
fade_px: int = 8,
|
| 98 |
+
) -> tuple[np.ndarray, int, int]:
|
| 99 |
+
"""Pipeline completa da immagine a heightmap normalizzata [0,1]."""
|
| 100 |
+
gray = load_and_prepare(image_path, max_px)
|
| 101 |
+
h, w = gray.shape
|
| 102 |
+
|
| 103 |
+
gray = smooth_heightmap(gray, blur)
|
| 104 |
+
|
| 105 |
+
# Gamma correction
|
| 106 |
+
gray = np.power(gray / 255.0, gamma) * 255.0
|
| 107 |
+
|
| 108 |
+
if quantize >= 2:
|
| 109 |
+
gray = quantize_heightmap(gray, quantize)
|
| 110 |
+
|
| 111 |
+
if tileable:
|
| 112 |
+
gray = make_tileable(gray, fade_px)
|
| 113 |
+
|
| 114 |
+
if invert:
|
| 115 |
+
gray = 255.0 - gray
|
| 116 |
+
|
| 117 |
+
gray = np.clip(gray, 0, 255)
|
| 118 |
+
|
| 119 |
+
# Normalizza [0, 1]
|
| 120 |
+
height_map = gray / 255.0
|
| 121 |
+
return height_map, w, h
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# ─────────────────────────────────────────────
|
| 125 |
+
# STL GENERATION
|
| 126 |
+
# ─────────────────────────────────────────────
|
| 127 |
+
|
| 128 |
+
def _pack_triangle(normal, v1, v2, v3) -> bytes:
|
| 129 |
+
return struct.pack(
|
| 130 |
+
"<ffffffffffffH",
|
| 131 |
+
*normal, *v1, *v2, *v3, 0
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def heightmap_to_stl(
|
| 136 |
+
height_map: np.ndarray,
|
| 137 |
+
px_w: int,
|
| 138 |
+
px_h: int,
|
| 139 |
+
tile_w_mm: float,
|
| 140 |
+
tile_h_mm: float,
|
| 141 |
+
relief_mm: float,
|
| 142 |
+
base_mm: float,
|
| 143 |
+
) -> bytes:
|
| 144 |
+
"""
|
| 145 |
+
Genera un file STL binario da una heightmap normalizzata [0,1].
|
| 146 |
+
|
| 147 |
+
La superficie superiore è il rilievo.
|
| 148 |
+
La base è piatta a z=0 (superficie inferiore a z=-base_mm internamente,
|
| 149 |
+
ma tutto shiftato così il bottom è a z=0 e il rilievo è in positivo).
|
| 150 |
+
"""
|
| 151 |
+
pixel_x = tile_w_mm / (px_w - 1)
|
| 152 |
+
pixel_y = tile_h_mm / (px_h - 1)
|
| 153 |
+
|
| 154 |
+
# z assoluto di ogni pixel della superficie
|
| 155 |
+
# base a z=0, rilievo sale fino a relief_mm
|
| 156 |
+
def z(row, col):
|
| 157 |
+
return base_mm + height_map[row, col] * relief_mm
|
| 158 |
+
|
| 159 |
+
triangles = []
|
| 160 |
+
|
| 161 |
+
# ── Superficie superiore (rilievo) ──
|
| 162 |
+
for row in range(px_h - 1):
|
| 163 |
+
for col in range(px_w - 1):
|
| 164 |
+
x0, x1 = col * pixel_x, (col + 1) * pixel_x
|
| 165 |
+
y0, y1 = (px_h - 1 - row) * pixel_y, (px_h - 2 - row) * pixel_y
|
| 166 |
+
|
| 167 |
+
z00 = z(row, col)
|
| 168 |
+
z10 = z(row, col + 1)
|
| 169 |
+
z01 = z(row + 1, col)
|
| 170 |
+
z11 = z(row + 1, col + 1)
|
| 171 |
+
|
| 172 |
+
v00 = (x0, y0, z00)
|
| 173 |
+
v10 = (x1, y0, z10)
|
| 174 |
+
v01 = (x0, y1, z01)
|
| 175 |
+
v11 = (x1, y1, z11)
|
| 176 |
+
|
| 177 |
+
# Triangolo 1
|
| 178 |
+
e1 = np.subtract(v10, v00)
|
| 179 |
+
e2 = np.subtract(v01, v00)
|
| 180 |
+
n = np.cross(e1, e2)
|
| 181 |
+
nn = n / (np.linalg.norm(n) + 1e-12)
|
| 182 |
+
triangles.append(_pack_triangle(nn, v00, v10, v01))
|
| 183 |
+
|
| 184 |
+
# Triangolo 2
|
| 185 |
+
e1 = np.subtract(v01, v11)
|
| 186 |
+
e2 = np.subtract(v10, v11)
|
| 187 |
+
n = np.cross(e1, e2)
|
| 188 |
+
nn = n / (np.linalg.norm(n) + 1e-12)
|
| 189 |
+
triangles.append(_pack_triangle(nn, v11, v01, v10))
|
| 190 |
+
|
| 191 |
+
# ── Base inferiore (piatta a z=0) ──
|
| 192 |
+
for row in range(px_h - 1):
|
| 193 |
+
for col in range(px_w - 1):
|
| 194 |
+
x0, x1 = col * pixel_x, (col + 1) * pixel_x
|
| 195 |
+
y0, y1 = (px_h - 1 - row) * pixel_y, (px_h - 2 - row) * pixel_y
|
| 196 |
+
n = (0.0, 0.0, -1.0)
|
| 197 |
+
triangles.append(_pack_triangle(n, (x0, y0, 0), (x0, y1, 0), (x1, y0, 0)))
|
| 198 |
+
triangles.append(_pack_triangle(n, (x1, y1, 0), (x1, y0, 0), (x0, y1, 0)))
|
| 199 |
+
|
| 200 |
+
W = tile_w_mm
|
| 201 |
+
H = tile_h_mm
|
| 202 |
+
|
| 203 |
+
# ── Pareti laterali ──
|
| 204 |
+
# Fronte (y=0)
|
| 205 |
+
for col in range(px_w - 1):
|
| 206 |
+
x0, x1 = col * pixel_x, (col + 1) * pixel_x
|
| 207 |
+
z0_top = z(px_h - 1, col)
|
| 208 |
+
z1_top = z(px_h - 1, col + 1)
|
| 209 |
+
n = (0.0, -1.0, 0.0)
|
| 210 |
+
triangles.append(_pack_triangle(n, (x0, 0, 0), (x1, 0, z1_top), (x0, 0, z0_top)))
|
| 211 |
+
triangles.append(_pack_triangle(n, (x0, 0, 0), (x1, 0, 0), (x1, 0, z1_top)))
|
| 212 |
+
|
| 213 |
+
# Retro (y=H)
|
| 214 |
+
for col in range(px_w - 1):
|
| 215 |
+
x0, x1 = col * pixel_x, (col + 1) * pixel_x
|
| 216 |
+
z0_top = z(0, col)
|
| 217 |
+
z1_top = z(0, col + 1)
|
| 218 |
+
n = (0.0, 1.0, 0.0)
|
| 219 |
+
triangles.append(_pack_triangle(n, (x0, H, z0_top), (x1, H, z1_top), (x0, H, 0)))
|
| 220 |
+
triangles.append(_pack_triangle(n, (x1, H, z1_top), (x1, H, 0), (x0, H, 0)))
|
| 221 |
+
|
| 222 |
+
# Sinistra (x=0)
|
| 223 |
+
for row in range(px_h - 1):
|
| 224 |
+
y0 = (px_h - 1 - row) * pixel_y
|
| 225 |
+
y1 = (px_h - 2 - row) * pixel_y
|
| 226 |
+
z0_top = z(row, 0)
|
| 227 |
+
z1_top = z(row + 1, 0)
|
| 228 |
+
n = (-1.0, 0.0, 0.0)
|
| 229 |
+
triangles.append(_pack_triangle(n, (0, y0, z0_top), (0, y1, z1_top), (0, y0, 0)))
|
| 230 |
+
triangles.append(_pack_triangle(n, (0, y1, 0), (0, y0, 0), (0, y1, z1_top)))
|
| 231 |
+
|
| 232 |
+
# Destra (x=W)
|
| 233 |
+
for row in range(px_h - 1):
|
| 234 |
+
y0 = (px_h - 1 - row) * pixel_y
|
| 235 |
+
y1 = (px_h - 2 - row) * pixel_y
|
| 236 |
+
z0_top = z(row, px_w - 1)
|
| 237 |
+
z1_top = z(row + 1, px_w - 1)
|
| 238 |
+
n = (1.0, 0.0, 0.0)
|
| 239 |
+
triangles.append(_pack_triangle(n, (W, y0, 0), (W, y1, z1_top), (W, y0, z0_top)))
|
| 240 |
+
triangles.append(_pack_triangle(n, (W, y0, 0), (W, y1, 0), (W, y1, z1_top)))
|
| 241 |
+
|
| 242 |
+
header = b"\x00" * 80
|
| 243 |
+
count = struct.pack("<I", len(triangles))
|
| 244 |
+
return header + count + b"".join(triangles)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def save_depth_preview(height_map: np.ndarray, out_path: str):
|
| 248 |
+
"""Salva la depth map come PNG per verifica visiva."""
|
| 249 |
+
preview = (height_map * 255).astype(np.uint8)
|
| 250 |
+
Image.fromarray(preview).save(out_path)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
# ─────────────────────────────────────────────
|
| 254 |
+
# CLI
|
| 255 |
+
# ─────────────────────────────────────────────
|
| 256 |
+
|
| 257 |
+
def main():
|
| 258 |
+
parser = argparse.ArgumentParser(
|
| 259 |
+
description="Image → 3D relief tile STL — Fabrication Topologies workshop",
|
| 260 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 261 |
+
epilog="""
|
| 262 |
+
Parametri chiave:
|
| 263 |
+
--width / --height dimensioni della tile in mm (default 100×100)
|
| 264 |
+
--relief altezza massima del rilievo in mm (default 4)
|
| 265 |
+
--base spessore della base piatta in mm (default 2)
|
| 266 |
+
--invert inverte: scuro=alto invece di bianco=alto
|
| 267 |
+
--quantize N quantizza in N livelli di altezza (effetto a gradini)
|
| 268 |
+
es: --quantize 4 → 4 livelli netti, bello per pattern urbani
|
| 269 |
+
--tileable sfuma i bordi per affiancamento senza giunti visibili
|
| 270 |
+
--blur forza del smoothing (default 0.5, 0=nessuno, 2=molto)
|
| 271 |
+
--resolution max pixel per lato (default 500, più alto = più dettaglio ma più lento)
|
| 272 |
+
--preview salva anche la depth map come PNG per verifica
|
| 273 |
+
""",
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
parser.add_argument("input", help="Immagine di input (PNG, JPG, ...)")
|
| 277 |
+
parser.add_argument("--output", "-o", help="File STL di output (default: input_relief.stl)")
|
| 278 |
+
parser.add_argument("--width", type=float, default=100.0, help="Larghezza tile in mm (default 100)")
|
| 279 |
+
parser.add_argument("--height", type=float, default=100.0, help="Altezza tile in mm (default 100, 0=proporzioni originali)")
|
| 280 |
+
parser.add_argument("--relief", type=float, default=4.0, help="Spessore massimo rilievo in mm (default 4)")
|
| 281 |
+
parser.add_argument("--base", type=float, default=2.0, help="Spessore base piatta in mm (default 2)")
|
| 282 |
+
parser.add_argument("--invert", action="store_true", help="Inverte: scuro=alto, bianco=basso")
|
| 283 |
+
parser.add_argument("--quantize", type=int, default=0, metavar="N", help="Quantizza in N livelli (0=continuo)")
|
| 284 |
+
parser.add_argument("--tileable", action="store_true", help="Sfuma i bordi per affiancamento")
|
| 285 |
+
parser.add_argument("--fade", type=int, default=12, help="Pixel di fade per --tileable (default 12)")
|
| 286 |
+
parser.add_argument("--blur", type=float, default=0.5, help="Smoothing (0=nessuno, 0.5=default, 2=molto)")
|
| 287 |
+
parser.add_argument("--gamma", type=float, default=1.1, help="Correzione contrasto (default 1.1)")
|
| 288 |
+
parser.add_argument("--resolution", type=int, default=500, help="Max pixel per lato (default 500)")
|
| 289 |
+
parser.add_argument("--preview", action="store_true", help="Salva depth map PNG per verifica")
|
| 290 |
+
|
| 291 |
+
args = parser.parse_args()
|
| 292 |
+
|
| 293 |
+
# Output path
|
| 294 |
+
input_path = Path(args.input)
|
| 295 |
+
if not input_path.exists():
|
| 296 |
+
print(f"Errore: file non trovato: {args.input}")
|
| 297 |
+
sys.exit(1)
|
| 298 |
+
|
| 299 |
+
output_path = Path(args.output) if args.output else input_path.with_name(input_path.stem + "_relief.stl")
|
| 300 |
+
|
| 301 |
+
print(f"Input: {input_path}")
|
| 302 |
+
print(f"Output: {output_path}")
|
| 303 |
+
print(f"Tile: {args.width:.1f} × {args.height:.1f} mm")
|
| 304 |
+
print(f"Rilievo: {args.relief:.1f} mm | Base: {args.base:.1f} mm")
|
| 305 |
+
print(f"Totale Z: {args.relief + args.base:.1f} mm")
|
| 306 |
+
print(f"Opzioni: invert={args.invert} quantize={args.quantize} tileable={args.tileable}")
|
| 307 |
+
print()
|
| 308 |
+
|
| 309 |
+
# Build heightmap
|
| 310 |
+
print("Elaborazione immagine...")
|
| 311 |
+
height_map, px_w, px_h = build_heightmap(
|
| 312 |
+
str(input_path),
|
| 313 |
+
max_px=args.resolution,
|
| 314 |
+
invert=args.invert,
|
| 315 |
+
blur=args.blur,
|
| 316 |
+
gamma=args.gamma,
|
| 317 |
+
quantize=args.quantize,
|
| 318 |
+
tileable=args.tileable,
|
| 319 |
+
fade_px=args.fade,
|
| 320 |
+
)
|
| 321 |
+
print(f"Risoluzione mesh: {px_w} × {px_h} px ({px_w * px_h:,} vertici)")
|
| 322 |
+
|
| 323 |
+
if args.preview:
|
| 324 |
+
preview_path = input_path.with_name(input_path.stem + "_depthmap.png")
|
| 325 |
+
save_depth_preview(height_map, str(preview_path))
|
| 326 |
+
print(f"Depth map salvata: {preview_path}")
|
| 327 |
+
|
| 328 |
+
# Calcola altezza tile se proporzioni originali
|
| 329 |
+
tile_h = args.height if args.height > 0 else args.width * px_h / px_w
|
| 330 |
+
|
| 331 |
+
# Generate STL
|
| 332 |
+
print("Generazione STL...")
|
| 333 |
+
stl_bytes = heightmap_to_stl(
|
| 334 |
+
height_map, px_w, px_h,
|
| 335 |
+
tile_w_mm=args.width,
|
| 336 |
+
tile_h_mm=tile_h,
|
| 337 |
+
relief_mm=args.relief,
|
| 338 |
+
base_mm=args.base,
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
with open(output_path, "wb") as f:
|
| 342 |
+
f.write(stl_bytes)
|
| 343 |
+
|
| 344 |
+
size_kb = len(stl_bytes) / 1024
|
| 345 |
+
print(f"\n✓ STL salvato: {output_path} ({size_kb:.0f} KB)")
|
| 346 |
+
print(f" Triangoli: {(len(stl_bytes) - 84) // 50:,}")
|
| 347 |
+
|
| 348 |
+
if size_kb > 30_000:
|
| 349 |
+
print(" Avviso: file grande, considera --resolution 300 per ridurre le dimensioni")
|
| 350 |
+
|
| 351 |
+
print()
|
| 352 |
+
print("Impostazioni slicer consigliate:")
|
| 353 |
+
print(" Layer height: 0.15 mm | Infill: 15% | No supports")
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
if __name__ == "__main__":
|
| 357 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0
|
| 2 |
+
numpy
|
| 3 |
+
pillow
|
| 4 |
+
opencv-python-headless
|
| 5 |
+
scipy
|