Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,76 @@
|
|
| 1 |
-
# app.py (
|
| 2 |
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
Beide Bilder werden auf die
|
| 13 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 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
|
| 60 |
-
description="
|
| 61 |
-
fn=
|
| 62 |
inputs=[
|
| 63 |
gr.Image(type="filepath", label="Bild 1 (Basisbild)"),
|
| 64 |
-
gr.Image(type="filepath", label="Bild 2 (
|
| 65 |
-
gr.
|
|
|
|
| 66 |
],
|
| 67 |
outputs=[
|
| 68 |
-
gr.Image(label="
|
| 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)
|