Spaces:
Running
Running
Download app.py from bokula/pattern-ground: direct link, hf CLI and curl.
- Browser
- Download file 9.39 kB
-
https://huggingface.co/spaces/bokula/pattern-ground/resolve/main/app.py
- Command line
-
hf download hf://spaces/bokula/pattern-ground/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bokula/pattern-ground/resolve/main/app.py
9.39 kB
| """ | |
| 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() | |