Text-Image_to_Image / text_integration.py
Astridkraft's picture
Update text_integration.py
500a532 verified
raw
history blame
5.23 kB
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