Spaces:
Running
Running
File size: 9,391 Bytes
5a5f252 d6e17ea 1baefa4 5a5f252 1863a65 5a5f252 1863a65 5a5f252 d6e17ea 5a5f252 d6e17ea 1863a65 5a5f252 d6e17ea 5a5f252 d6e17ea 1baefa4 1863a65 5a5f252 d6e17ea 5a5f252 d6e17ea 1863a65 5a5f252 1863a65 5a5f252 1863a65 1baefa4 1863a65 5a5f252 1863a65 5a5f252 d6e17ea 5a5f252 1863a65 1baefa4 5a5f252 1baefa4 5a5f252 45d0740 d6e17ea 45d0740 5a5f252 d6e17ea 5a5f252 45d0740 d6e17ea 5a5f252 1baefa4 5a5f252 1baefa4 58b1e11 1863a65 1baefa4 5a5f252 d6e17ea 1baefa4 d6e17ea 1baefa4 d6e17ea 1863a65 58b1e11 d6e17ea 1863a65 1baefa4 d6e17ea 1863a65 d6e17ea 1863a65 d6e17ea 1863a65 d6e17ea 5a5f252 d6e17ea 5a5f252 d6e17ea | 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | """
app.py — Pattern Ground Relief Generator
Gradio 5.x — Python 3.13 — HuggingFace Spaces
"""
import tempfile
from pathlib import Path
import cv2
import gradio as gr
import numpy as np
from PIL import Image
from relief_workshop import build_heightmap, heightmap_to_stl
def preview_depthmap(image, invert, quantize_levels, tileable, blur):
if image is None:
return None, "⚠️ Carica un'immagine."
try:
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
tmp = f.name
Image.fromarray(image).save(tmp)
quantize = int(quantize_levels) if quantize_levels >= 2 else 0
height_map, px_w, px_h = build_heightmap(
tmp, max_px=300, invert=invert, blur=blur, gamma=1.1,
quantize=quantize, tileable=tileable, fade_px=12,
)
Path(tmp).unlink(missing_ok=True)
preview = (height_map * 255).astype(np.uint8)
msg = f"Depth map {px_w}×{px_h}px — bianco=alto, nero=basso"
if invert:
msg += " (invertita)"
return preview, msg
except Exception as e:
return None, f"❌ {e}"
def generate_stl(
image, tile_w, tile_h_mode, tile_h_custom,
relief_mm, base_mm,
invert, quantize_levels, tileable, blur, resolution,
):
if image is None:
return None, None, "⚠️ Carica un'immagine."
try:
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as f:
tmp_in = f.name
Image.fromarray(image).save(tmp_in)
img_h, img_w = image.shape[:2]
if tile_h_mode == "Quadrata (uguale alla larghezza)":
tile_h = tile_w
elif tile_h_mode == "Proporzioni originali":
tile_h = tile_w * img_h / img_w
else:
tile_h = tile_h_custom
quantize = int(quantize_levels) if quantize_levels >= 2 else 0
height_map, px_w, px_h = build_heightmap(
tmp_in, max_px=int(resolution),
invert=invert, blur=blur, gamma=1.1,
quantize=quantize, tileable=tileable, fade_px=12,
)
Path(tmp_in).unlink(missing_ok=True)
preview = (height_map * 255).astype(np.uint8)
stl_bytes = heightmap_to_stl(
height_map, px_w, px_h,
tile_w_mm=float(tile_w),
tile_h_mm=float(tile_h),
relief_mm=float(relief_mm),
base_mm=float(base_mm),
)
stl_tmp = tempfile.NamedTemporaryFile(suffix=".stl", delete=False, prefix="tile_")
stl_tmp.write(stl_bytes)
stl_tmp.close()
n_tri = (len(stl_bytes) - 84) // 50
size_kb = len(stl_bytes) / 1024
info = (
f"✓ Tile generata\n\n"
f"Dimensioni: {tile_w:.0f} × {tile_h:.0f} mm\n"
f"Rilievo: {relief_mm:.1f} mm\n"
f"Base: {base_mm:.1f} mm\n"
f"Mesh: {px_w}×{px_h}px — {n_tri:,} triangoli\n"
f"File: {size_kb:.0f} KB\n\n"
f"Slicer: layer 0.15 mm · infill 15% · no supports"
)
return preview, stl_tmp.name, info
except Exception as e:
return None, None, f"❌ {e}"
def toggle_custom(mode):
return gr.update(visible=(mode == "Personalizzata"))
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Mono:ital,wght@0,400;0,500;1,400&display=swap');
body { background: #ffffff !important; }
.gradio-container {
background: #ffffff !important;
font-family: 'DM Mono', monospace !important;
max-width: 1100px !important;
}
.pg-header { padding: 24px 0 18px; border-bottom: 1px solid #e0ddd5; margin-bottom: 4px; }
.pg-logo { font-family: 'Bebas Neue', sans-serif; font-size: 56px; letter-spacing: 0.1em; color: #1a1a1a; line-height: 1; display: block; }
.pg-logo .accent { color: #b8860b; }
.pg-sub { font-family: 'DM Mono', monospace; font-size: 9px; letter-spacing: 0.22em; color: #1a1a1a; text-transform: uppercase; margin-top: 8px; display: block; }
.pg-rule { width: 100%; height: 1px; background: #e0ddd5; margin-top: 18px; }
.gradio-container h2 {
font-family: 'DM Mono', monospace !important;
font-size: 9px !important;
letter-spacing: 0.2em !important;
color: #b8860b !important;
text-transform: uppercase !important;
border-bottom: 1px solid #e0ddd5 !important;
padding-bottom: 6px !important;
margin-top: 20px !important;
font-weight: 400 !important;
}
button.primary {
background: #1a1a1a !important;
color: #fff !important;
border: none !important;
border-radius: 0 !important;
font-family: 'DM Mono', monospace !important;
text-transform: uppercase !important;
letter-spacing: 0.1em !important;
font-size: 0.7rem !important;
font-weight: 500 !important;
}
button.secondary {
background: transparent !important;
color: #888 !important;
border: 1px solid #ccc !important;
border-radius: 0 !important;
font-family: 'DM Mono', monospace !important;
text-transform: uppercase !important;
letter-spacing: 0.1em !important;
font-size: 0.7rem !important;
}
button:hover { opacity: 0.75 !important; }
label span {
font-family: 'DM Mono', monospace !important;
font-size: 10px !important;
letter-spacing: 0.1em !important;
color: #888 !important;
text-transform: uppercase !important;
}
input[type=range] { accent-color: #1a1a1a !important; }
input[type=checkbox] { accent-color: #1a1a1a !important; }
textarea {
background: #fafaf8 !important;
border: 1px solid #e0ddd5 !important;
color: #333 !important;
font-family: 'DM Mono', monospace !important;
font-size: 12px !important;
border-radius: 0 !important;
}
.guide { font-family: 'DM Mono', monospace; font-size: 11px; line-height: 1.9; color: #aaa; margin-top: 16px; }
.guide strong { color: #555; font-weight: 500; }
"""
HEADER_HTML = """
<div class="pg-header">
<span class="pg-logo">Pattern <span class="accent">Ground</span></span>
<span class="pg-sub">Fabrication Topologies · Vectorealism × Dropcity · Design Week Milano 2026</span>
<div class="pg-rule"></div>
</div>
"""
GUIDE_HTML = """
<div class="guide">
<strong>bianco = alto, nero = basso</strong> — controlla la depth map prima di generare.<br><br>
<strong>Quantize</strong> — livelli netti invece di gradiente continuo.<br>
Ottimo per pattern urbani: sanpietrini, griglia, muratura.<br><br>
<strong>Tileable</strong> — bordi sfumati per affiancamento senza giunti.<br><br>
<strong>Slicer:</strong> layer 0.15 mm · infill 15% · no supports · ironing top
</div>
"""
with gr.Blocks(css=CSS, title="Pattern Ground") as demo:
gr.HTML(HEADER_HTML)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## Immagine")
image_in = gr.Image(label="Carica immagine", type="numpy", image_mode="RGB")
gr.Markdown("## Dimensioni tile")
tile_w = gr.Slider(20, 200, value=100, step=5, label="Larghezza (mm)")
tile_h_mode = gr.Radio(
["Quadrata (uguale alla larghezza)", "Proporzioni originali", "Personalizzata"],
value="Quadrata (uguale alla larghezza)", label="Altezza",
)
tile_h_custom = gr.Slider(20, 200, value=100, step=5,
label="Altezza personalizzata (mm)", visible=False)
gr.Markdown("## Rilievo")
with gr.Row():
relief_mm = gr.Slider(1, 10, value=4, step=0.5, label="Altezza rilievo (mm)")
base_mm = gr.Slider(0.5, 5, value=2, step=0.5, label="Spessore base (mm)")
gr.Markdown("## Processing")
quantize = gr.Slider(0, 12, value=0, step=1,
label="Quantize — livelli (0=continuo, 4–6=gradini)")
blur = gr.Slider(0, 3, value=0.5, step=0.1, label="Smoothing (0=nessuno)")
resolution = gr.Slider(100, 400, value=200, step=50,
label="Risoluzione mesh (px per lato)")
gr.Markdown("## Opzioni")
with gr.Row():
invert = gr.Checkbox(label="Invert (scuro=alto)", value=False)
tileable = gr.Checkbox(label="Tileable (bordi sfumati)", value=False)
with gr.Row():
preview_btn = gr.Button("Depth map preview", variant="secondary")
gen_btn = gr.Button("Genera STL", variant="primary")
with gr.Column(scale=1):
gr.Markdown("## Depth map")
depth_out = gr.Image(label="Bianco = alto · Nero = basso",
type="numpy", interactive=False)
gr.Markdown("## Download")
stl_out = gr.File(label="STL file")
gr.Markdown("## Info")
info_out = gr.Textbox(label="", lines=7, interactive=False)
gr.HTML(GUIDE_HTML)
tile_h_mode.change(toggle_custom, tile_h_mode, tile_h_custom)
preview_inputs = [image_in, invert, quantize, tileable, blur]
gen_inputs = [
image_in, tile_w, tile_h_mode, tile_h_custom,
relief_mm, base_mm, invert, quantize, tileable, blur, resolution,
]
preview_btn.click(preview_depthmap, preview_inputs, [depth_out, info_out])
gen_btn.click(generate_stl, gen_inputs, [depth_out, stl_out, info_out])
if __name__ == "__main__":
demo.launch()
|