Alibrown commited on
Commit
7850adf
·
verified ·
1 Parent(s): 6cddf5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -1,27 +1,18 @@
1
- # app.py (Mit Blend- und Stack-Funktion)
2
-
3
  from PIL import Image
4
  import gradio as gr
5
  import numpy as np
6
- # Wir brauchen NumPy für das horizontale Stapeln (wird von PIL nicht direkt unterstützt)
7
 
8
- # 1. Stacking-Funktion
9
  def stack_horizontal(img1_path, img2_path):
10
- """
11
- Stapelt zwei Bilder horizontal nebeneinander.
12
- Beide Bilder werden auf die Höhe des ersten Bildes skaliert.
13
- """
14
  img1 = Image.open(img1_path).convert("RGB")
15
  img2 = Image.open(img2_path).convert("RGB")
16
 
17
- # 1. Höhe angleichen (Voraussetzung für horizontales Stapeln)
18
  h1, h2 = img1.size[1], img2.size[1]
19
  if h1 != h2:
20
- # Skaliere img2 auf die Höhe von img1, behalte Seitenverhältnis
21
  w2_new = int(img2.size[0] * h1 / h2)
22
- img2 = img2.resize((w2_new, h1))
23
 
24
- # 2. Neues Bild erstellen und Bilder hineinkopieren
25
  total_width = img1.size[0] + img2.size[0]
26
  total_height = img1.size[1]
27
 
@@ -31,46 +22,53 @@ def stack_horizontal(img1_path, img2_path):
31
 
32
  return stacked_img
33
 
34
- # 2. Blending-Funktion (wie zuvor)
35
  def blend_images_cpu(image1_path, image2_path, alpha: float):
 
36
  img1 = Image.open(image1_path).convert("RGBA")
37
  img2 = Image.open(image2_path).convert("RGBA")
38
 
39
  if img1.size != img2.size:
40
- img2 = img2.resize(img1.size)
41
-
42
- return Image.blend(img1, img2, alpha)
43
-
44
-
45
- # 3. Haupt-Inferenzfunktion
46
- def infer(image1_path, image2_path, method, alpha: float):
47
- if method == "Blend":
48
- # Blending-Faktor (Alpha) wird nur bei Blend genutzt
49
- return blend_images_cpu(image1_path, image2_path, alpha)
50
- elif method == "Stack Horizontal":
51
- # Alpha-Faktor wird ignoriert
52
- return stack_horizontal(image1_path, image2_path)
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- # Gradio Interface erstellen
56
  if __name__ == "__main__":
57
-
58
- # Liste der verfügbaren Methoden
59
  methods = ["Blend", "Stack Horizontal"]
60
 
61
- # HINWEIS: gr.Examples ist hier NICHT enthalten, um Startprobleme zu vermeiden!
62
- gr.Interface(
63
- title="CPU Image Transformer (Blend & Stack)",
64
- description="Wähle zwischen Blending (Überblenden mit Alpha) oder Horizontalem Stacking (Aneinanderreihen). Reine CPU-Operation.",
65
- fn=infer,
66
- inputs=[
67
- gr.Image(type="filepath", label="Bild 1 (Basisbild)"),
68
- gr.Image(type="filepath", label="Bild 2 (Zusatzbild)"),
69
- gr.Dropdown(methods, label="Transformationsmethode", value="Blend"),
70
- gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="Blending-Faktor (Alpha, nur bei 'Blend' aktiv)")
71
- ],
72
- outputs=[
73
- gr.Image(label="Resultat")
74
- ],
75
- cache_examples=False,
76
- ).queue().launch(share=False)
 
 
 
 
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:
 
13
  w2_new = int(img2.size[0] * h1 / h2)
14
+ img2 = img2.resize((w2_new, h1), Image.Resampling.LANCZOS)
15
 
 
16
  total_width = img1.size[0] + img2.size[0]
17
  total_height = img1.size[1]
18
 
 
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)