File size: 3,831 Bytes
cc9e326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbbc845
5f445bd
cbbc845
cc9e326
5f445bd
cbbc845
7850adf
cbbc845
 
6cddf5c
 
 
 
7850adf
6cddf5c
 
 
5f445bd
6cddf5c
 
 
 
 
5f445bd
cbbc845
7850adf
cbbc845
 
6cddf5c
 
7850adf
 
 
 
f1cb255
cbbc845
 
 
 
 
 
 
f1cb255
cbbc845
 
 
 
6cddf5c
cbbc845
7850adf
cbbc845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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!")