# app.py - Streamlit für Hugging Face Spaces import os import tempfile # ---------------------------------------------------- # 🚨 KRITISCHE FIXES FÜR DEN PERMISSION ERROR # Zwingt Streamlit, seine temporären/Konfigurationsdateien # in den beschreibbaren /tmp-Bereich zu schreiben. # ---------------------------------------------------- # 1. Temporären, beschreibbaren Pfad erstellen TEMP_STREAMLIT_HOME = os.path.join(tempfile.gettempdir(), "st_config_workaround") os.makedirs(TEMP_STREAMLIT_HOME, exist_ok=True) # 2. Umgebungsvariablen setzen os.environ["STREAMLIT_HOME"] = TEMP_STREAMLIT_HOME os.environ["STREAMLIT_GATHER_USAGE_STATS"] = "false" # 3. Minimale config.toml erstellen, um Schreibversuche zu unterbinden CONFIG_PATH = os.path.join(TEMP_STREAMLIT_HOME, "config.toml") CONFIG_CONTENT = """ [browser] gatherUsageStats = false [server] headless = true port = 7860 enableCORS = false """ with open(CONFIG_PATH, "w") as f: f.write(CONFIG_CONTENT) # JETZT erst Streamlit importieren! import streamlit as st from PIL import Image import io import numpy as np def stack_horizontal(img1, img2): """Stapelt zwei Bilder horizontal nebeneinander.""" img1 = img1.convert("RGB") img2 = img2.convert("RGB") h1, h2 = img1.size[1], img2.size[1] if h1 != h2: w2_new = int(img2.size[0] * h1 / h2) img2 = img2.resize((w2_new, h1), Image.Resampling.LANCZOS) total_width = img1.size[0] + img2.size[0] total_height = img1.size[1] stacked_img = Image.new('RGB', (total_width, total_height)) stacked_img.paste(img1, (0, 0)) stacked_img.paste(img2, (img1.size[0], 0)) return stacked_img def blend_images_cpu(img1, img2, alpha): """Blendet zwei Bilder mit Alpha-Faktor.""" img1 = img1.convert("RGBA") img2 = img2.convert("RGBA") if img1.size != img2.size: img2 = img2.resize(img1.size, Image.Resampling.LANCZOS) blended = Image.blend(img1, img2, alpha) return blended.convert("RGB") # Streamlit UI st.set_page_config(page_title="Image Transformer", layout="wide") st.title("🖼️ CPU Image Transformer") st.markdown("**Blend** oder **Stack** - Reine CPU-Operation") col1, col2 = st.columns(2) with col1: st.subheader("Eingabe") img1_file = st.file_uploader("Bild 1 (Basisbild)", type=["png", "jpg", "jpeg", "webp"]) img2_file = st.file_uploader("Bild 2 (Zusatzbild)", type=["png", "jpg", "jpeg", "webp"]) method = st.selectbox("Transformationsmethode", ["Blend", "Stack Horizontal"]) alpha = st.slider("Blending-Faktor (nur bei Blend)", 0.0, 1.0, 0.5, 0.05) process_btn = st.button("🚀 Verarbeiten", type="primary") with col2: st.subheader("Resultat") result_placeholder = st.empty() # Verarbeitung if process_btn: if img1_file and img2_file: with st.spinner("Verarbeite..."): try: img1 = Image.open(img1_file) img2 = Image.open(img2_file) if method == "Blend": result = blend_images_cpu(img1, img2, alpha) else: result = stack_horizontal(img1, img2) result_placeholder.image(result, use_container_width=True) # Download-Button buf = io.BytesIO() result.save(buf, format="PNG") st.download_button( label="💾 Download Resultat", data=buf.getvalue(), file_name=f"result_{method.lower().replace(' ', '_')}.png", mime="image/png" ) except Exception as e: st.error(f"Fehler: {e}") else: st.warning("⚠️ Bitte beide Bilder hochladen!")