Alibrown commited on
Commit
cbbc845
·
verified ·
1 Parent(s): ec1f48e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -45
app.py CHANGED
@@ -1,12 +1,12 @@
1
- # app.py - Fixed Version
 
2
  from PIL import Image
3
- import gradio as gr
4
- import numpy as np
5
 
6
- def stack_horizontal(img1_path, img2_path):
7
  """Stapelt zwei Bilder horizontal nebeneinander."""
8
- img1 = Image.open(img1_path).convert("RGB")
9
- img2 = Image.open(img2_path).convert("RGB")
10
 
11
  h1, h2 = img1.size[1], img2.size[1]
12
  if h1 != h2:
@@ -22,53 +22,66 @@ def stack_horizontal(img1_path, img2_path):
22
 
23
  return stacked_img
24
 
25
- def blend_images_cpu(image1_path, image2_path, alpha: float):
26
  """Blendet zwei Bilder mit Alpha-Faktor."""
27
- img1 = Image.open(image1_path).convert("RGBA")
28
- img2 = Image.open(image2_path).convert("RGBA")
29
 
30
  if img1.size != img2.size:
31
  img2 = img2.resize(img1.size, Image.Resampling.LANCZOS)
32
 
33
- # Blend gibt RGBA zurück, konvertiere zu RGB für konsistente Ausgabe
34
  blended = Image.blend(img1, img2, alpha)
35
  return blended.convert("RGB")
36
 
37
- def infer(image1, image2, method, alpha):
38
- """Haupt-Inferenzfunktion."""
39
- # Gradio gibt manchmal None zurück wenn kein Bild hochgeladen wurde
40
- if image1 is None or image2 is None:
41
- return None
42
-
43
- try:
44
- if method == "Blend":
45
- return blend_images_cpu(image1, image2, alpha)
46
- elif method == "Stack Horizontal":
47
- return stack_horizontal(image1, image2)
48
- except Exception as e:
49
- print(f"Fehler bei der Verarbeitung: {e}")
50
- return None
51
 
52
- # Gradio Interface
53
- if __name__ == "__main__":
54
- methods = ["Blend", "Stack Horizontal"]
 
55
 
56
- with gr.Blocks(title="CPU Image Transformer") as demo:
57
- gr.Markdown("# CPU Image Transformer (Blend & Stack)")
58
- gr.Markdown("Wähle zwischen Blending oder Horizontalem Stacking. Reine CPU-Operation.")
59
-
60
- with gr.Row():
61
- with gr.Column():
62
- img1 = gr.Image(type="filepath", label="Bild 1 (Basisbild)")
63
- img2 = gr.Image(type="filepath", label="Bild 2 (Zusatzbild)")
64
- method = gr.Dropdown(methods, label="Transformationsmethode", value="Blend")
65
- alpha = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5,
66
- label="Blending-Faktor (nur bei 'Blend')")
67
- btn = gr.Button("Verarbeiten")
68
-
69
- with gr.Column():
70
- output = gr.Image(label="Resultat")
71
-
72
- btn.click(fn=infer, inputs=[img1, img2, method, alpha], outputs=output)
73
 
74
- demo.queue().launch(share=False, server_name="127.0.0.1", server_port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py - Streamlit Version
2
+ import streamlit as st
3
  from PIL import Image
4
+ import io
 
5
 
6
+ def stack_horizontal(img1, img2):
7
  """Stapelt zwei Bilder horizontal nebeneinander."""
8
+ img1 = img1.convert("RGB")
9
+ img2 = img2.convert("RGB")
10
 
11
  h1, h2 = img1.size[1], img2.size[1]
12
  if h1 != h2:
 
22
 
23
  return stacked_img
24
 
25
+ def blend_images_cpu(img1, img2, alpha):
26
  """Blendet zwei Bilder mit Alpha-Faktor."""
27
+ img1 = img1.convert("RGBA")
28
+ img2 = img2.convert("RGBA")
29
 
30
  if img1.size != img2.size:
31
  img2 = img2.resize(img1.size, Image.Resampling.LANCZOS)
32
 
 
33
  blended = Image.blend(img1, img2, alpha)
34
  return blended.convert("RGB")
35
 
36
+ # Streamlit UI
37
+ st.set_page_config(page_title="Image Transformer", layout="wide")
38
+
39
+ st.title("🖼️ CPU Image Transformer")
40
+ st.markdown("**Blend** oder **Stack** - Reine CPU-Operation")
41
+
42
+ col1, col2 = st.columns(2)
 
 
 
 
 
 
 
43
 
44
+ with col1:
45
+ st.subheader("Eingabe")
46
+ img1_file = st.file_uploader("Bild 1 (Basisbild)", type=["png", "jpg", "jpeg", "webp"])
47
+ img2_file = st.file_uploader("Bild 2 (Zusatzbild)", type=["png", "jpg", "jpeg", "webp"])
48
 
49
+ method = st.selectbox("Transformationsmethode", ["Blend", "Stack Horizontal"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ alpha = st.slider("Blending-Faktor (nur bei Blend)", 0.0, 1.0, 0.5, 0.05)
52
+
53
+ process_btn = st.button("🚀 Verarbeiten", type="primary")
54
+
55
+ with col2:
56
+ st.subheader("Resultat")
57
+ result_placeholder = st.empty()
58
+
59
+ # Verarbeitung
60
+ if process_btn:
61
+ if img1_file and img2_file:
62
+ with st.spinner("Verarbeite..."):
63
+ try:
64
+ img1 = Image.open(img1_file)
65
+ img2 = Image.open(img2_file)
66
+
67
+ if method == "Blend":
68
+ result = blend_images_cpu(img1, img2, alpha)
69
+ else:
70
+ result = stack_horizontal(img1, img2)
71
+
72
+ result_placeholder.image(result, use_container_width=True)
73
+
74
+ # Download-Button
75
+ buf = io.BytesIO()
76
+ result.save(buf, format="PNG")
77
+ st.download_button(
78
+ label="💾 Download Resultat",
79
+ data=buf.getvalue(),
80
+ file_name=f"result_{method.lower().replace(' ', '_')}.png",
81
+ mime="image/png"
82
+ )
83
+
84
+ except Exception as e:
85
+ st.error(f"Fehler: {e}")
86
+ else:
87
+ st.warning("⚠️ Bitte beide Bilder hochladen!")