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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -50
app.py CHANGED
@@ -1,73 +1,76 @@
1
- # app.py (Korrigiert und erweitert)
2
 
3
  from PIL import Image
4
  import gradio as gr
5
  import numpy as np
6
- import os # Wird hinzugefügt, falls Beispielbilder im Ordner 'examples' liegen
7
 
8
- # Der Kern der Anwendung: Blending-Funktion
9
- def blend_images_cpu(image1_path, image2_path, alpha: float):
10
  """
11
- Verschmilzt zwei Bilder mit einem linearen Blending-Faktor (alpha).
12
- Beide Bilder werden auf die Größe des ersten Bildes skaliert.
13
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- try:
16
- # 1. Bilder laden und in den RGBA-Modus konvertieren (für sauberes Blending)
17
- img1 = Image.open(image1_path).convert("RGBA")
18
- img2 = Image.open(image2_path).convert("RGBA")
 
19
 
20
- # 2. Größe anpassen: img2 wird auf die Größe von img1 skaliert
21
- if img1.size != img2.size:
22
- # print("Größe der Bilder wird angepasst.") # Im Gradio-Space nicht sichtbar
23
- img2 = img2.resize(img1.size)
 
 
 
24
 
25
- # 3. Lineares Blending durchführen
26
- # Image.blend(Bild1, Bild2, alpha)
27
- # alpha=0.0 -> 100% Bild 1, alpha=1.0 -> 100% Bild 2
28
- blended_img = Image.blend(img1, img2, alpha)
29
 
30
- # Gradio kann direkt ein PIL Image-Objekt zurückgeben
31
- return blended_img
32
 
33
- except Exception as e:
34
- print(f"Ein Fehler ist aufgetreten: {e}")
35
- # Bei Fehler ein leeres Bild zurückgeben
36
- return np.zeros((200, 200, 3), dtype=np.uint8)
37
-
38
- # Liste der Beispiel-Eingaben
39
- # WICHTIG: Die Pfade müssen zu existierenden Dateien in deinem 'examples'-Ordner führen!
40
- # Annahme: Im Ordner 'examples' liegen 'vogel.jpg', 'wasser.jpg', 'muster.png'
41
- example_inputs = [
42
- # 1. Bild A dominant (Alpha = 0.2)
43
- ["examples/vogel.jpg", "examples/wasser.jpg", 0.2],
44
-
45
- # 2. 50/50 Mischung (Alpha = 0.5)
46
- ["examples/vogel.jpg", "examples/wasser.jpg", 0.5],
47
-
48
- # 3. Bild B dominant (Alpha = 0.8)
49
- ["examples/vogel.jpg", "examples/wasser.jpg", 0.8],
50
-
51
- # 4. Beispiel mit einem anderen Paar (Alpha = 0.4)
52
- ["examples/vogel.jpg", "examples/muster.png", 0.4]
53
- ]
54
 
55
 
56
  # Gradio Interface erstellen
57
  if __name__ == "__main__":
 
 
 
 
 
58
  gr.Interface(
59
- title="CPU-Friendly Image Blending (Pillow)",
60
- description="Lade zwei Bilder hoch und verschmelze sie mit einem einstellbaren Alpha-Faktor. **Alpha** steuert den Anteil von Bild 2: *0.0 = Nur Bild 1*, *1.0 = Nur Bild 2*",
61
- fn=blend_images_cpu,
62
  inputs=[
63
  gr.Image(type="filepath", label="Bild 1 (Basisbild)"),
64
- gr.Image(type="filepath", label="Bild 2 (Überlagerung)"),
65
- gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="Blending-Faktor (Alpha)")
 
66
  ],
67
  outputs=[
68
- gr.Image(label="Verschmolzenes Bild")
69
  ],
70
- # HIER IST DIE KORREKTUR: gr.Examples anstatt gr.Example
71
- examples=example_inputs,
72
  cache_examples=False,
73
- ).launch(share=False)
 
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
 
28
+ stacked_img = Image.new('RGB', (total_width, total_height))
29
+ stacked_img.paste(img1, (0, 0))
30
+ stacked_img.paste(img2, (img1.size[0], 0))
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)