Spaces:
Sleeping
Sleeping
File size: 5,225 Bytes
3f4f2ec ed46214 3f4f2ec ed46214 3f4f2ec 9d92c0c dddc7cb 9d92c0c 5a992e2 ed46214 dddc7cb 9d92c0c bbfe5e6 dddc7cb bbfe5e6 9d92c0c 5a992e2 9d92c0c 500a532 9d92c0c 500a532 9d92c0c 3f4f2ec bbfe5e6 3f4f2ec bbfe5e6 3f4f2ec |
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
import numpy as np
def add_text_to_image(image, text, text_x, text_y, font_size=36, font_color="#FFFFFF"):
"""Fügt Text mit PIL zum Bild hinzu"""
if image is None or not text:
return image
try:
# Konvertiere numpy array zu PIL Image falls nötig
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
image_with_text = image.copy()
draw = ImageDraw.Draw(image_with_text)
try:
font = ImageFont.truetype("arial.ttf", int(font_size))
except:
font = ImageFont.load_default()
draw.text((text_x, text_y), text, fill=font_color, font=font)
print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt")
return image_with_text
except Exception as e:
print(f"Fehler beim Text hinzufügen: {e}")
return image
def create_text_preview(image, text, text_x, text_y, font_size=36, font_color="#FFFFFF"):
"""Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
if image is None:
return image
try:
# Konvertiere numpy array zu PIL Image falls nötig
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Immer vom Originalbild starten, nicht von der vorherigen Vorschau
preview = image.copy()
draw = ImageDraw.Draw(preview)
# Nur roten Marker zeichnen wenn Text vorhanden UND Position nicht Standard (100,100)
if text and text_x is not None and text_y is not None and (text_x != 100 or text_y != 100):
# Rote Marker an Textposition
marker_radius = 8
marker_color = (255, 0, 0)
draw.ellipse([
text_x - marker_radius,
text_y - marker_radius,
text_x + marker_radius,
text_y + marker_radius
], fill=marker_color)
# Text zeichnen wenn vorhanden
if text:
try:
font = ImageFont.truetype("arial.ttf", int(font_size))
except:
font = ImageFont.load_default()
draw.text((text_x, text_y), text, fill=font_color, font=font)
return preview
except Exception as e:
print(f"Fehler bei Text-Vorschau: {e}")
return image
def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, target_selector):
"""Aktualisiert die Text-Vorschau für Bild-zu-Bild"""
if target_selector == "Originalbild":
target_image = original_image
else: # "Generiertes Bild"
target_image = generated_image
return create_text_preview(target_image, text, text_x, text_y)
def update_text_preview_t2i(image, text, text_x, text_y):
"""Aktualisiert die Text-Vorschau für Text-zu-Bild"""
return create_text_preview(image, text, text_x, text_y)
def capture_click(event: gr.SelectData):
"""Handhabt Klicks in beiden Tabs"""
if event and hasattr(event, 'index'):
print(f"🎯 CLICK ERFOLGREICH: ({event.index[0]}, {event.index[1]})")
return event.index[0], event.index[1]
print("❌ CLICK FEHLGESCHLAGEN - Event oder Index fehlt")
return 100, 100
def create_text_integration_section_t2i():
"""Erstellt die UI-Komponenten für Text-Integration in Text-zu-Bild"""
with gr.Row():
gr.Markdown("### 📝 Text auf Bild integrieren")
with gr.Row():
text_input_t2i = gr.Textbox(
label="Text eingeben",
placeholder="Dein Text hier...",
max_lines=2,
scale=3
)
with gr.Row():
# Start mit None statt 100, damit kein Marker an Standard-Position
text_x_t2i = gr.Number(value=None, visible=False)
text_y_t2i = gr.Number(value=None, visible=False)
with gr.Row():
text_btn_t2i = gr.Button("📝 Text auf generiertes Bild", variant="secondary")
return text_input_t2i, text_x_t2i, text_y_t2i, text_btn_t2i
def create_text_integration_section_i2i():
"""Erstellt die UI-Komponenten für Text-Integration in Bild-zu-Bild"""
with gr.Row():
gr.Markdown("### 📝 Text auf Bild integrieren")
with gr.Row():
text_input_i2i = gr.Textbox(
label="Text eingeben",
placeholder="Dein Text hier...",
max_lines=2,
scale=3
)
with gr.Row():
# Start mit None statt 100, damit kein Marker an Standard-Position
text_x_i2i = gr.Number(value=None, visible=False)
text_y_i2i = gr.Number(value=None, visible=False)
with gr.Row():
target_selector = gr.Radio(
choices=["Originalbild", "Generiertes Bild"],
value="Generiertes Bild",
label="Text auf welchem Bild?",
scale=2
)
with gr.Row():
text_btn_i2i = gr.Button("📝 Text integrieren", variant="secondary")
return text_input_i2i, text_x_i2i, text_y_i2i, target_selector, text_btn_i2i |