Spaces:
Running
Running
File size: 10,055 Bytes
83b2817 a19f80e 83b2817 85b8add 83b2817 85b8add 83b2817 85b8add 83b2817 85b8add 83b2817 85b8add 83b2817 85b8add 83b2817 85b8add 83b2817 4f59d72 | 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | from PIL import Image, ImageDraw, ImageFont
import gradio as gr
import numpy as np
import cv2
def add_text_to_image(image, text, text_x, text_y, font_size="80px", font_family="Standard", font_color="Weiß"):
"""Fügt Text mit OpenCV zum Bild hinzu - garantiert skalierbar! Mit PIL ging die Textintegration nicht da Pillow keine eigenen Fonts hat! Hätte Fonts nach Hugging Face uploaden müssen!"""
if image is None or not text:
return image
try:
# Konvertiere PIL Image zu OpenCV Format
if isinstance(image, np.ndarray):
opencv_image = image
else:
opencv_image = np.array(image)
opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR)
# Schriftgröße
try:
pixel_size = int(font_size.replace("px", ""))
# OpenCV Font-Skala anpassen (empirische Werte)
font_scale = pixel_size / 30.0
except:
font_scale = 2.0
# Schriftfarbe
color_mapping = {
"Weiß": (255, 255, 255),
"Schwarz": (0, 0, 0),
"Rot": (0, 0, 255)
}
actual_color = color_mapping.get(font_color, (255, 255, 255))
# OpenCV Font basierend auf Auswahl
font_mapping = {
"Standard": cv2.FONT_HERSHEY_SIMPLEX,
"Einfache Handschrift": cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
"Verschnörkelte Handschrift": cv2.FONT_HERSHEY_SCRIPT_COMPLEX
}
font = font_mapping.get(font_family, cv2.FONT_HERSHEY_SIMPLEX)
thickness = max(2, int(pixel_size / 20))
print(f"OpenCV Text: '{text}' an ({text_x}, {text_y}) - Schrift: {font_family}, Größe: {font_scale:.2f}, Dicke: {thickness}")
# Text mit OpenCV hinzufügen
cv2.putText(
opencv_image,
text,
(int(text_x), int(text_y)),
font,
font_scale,
actual_color,
thickness,
cv2.LINE_AA
)
# Zurück zu PIL Format
result_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
return Image.fromarray(result_image)
except Exception as e:
print(f"Fehler beim Text hinzufügen mit OpenCV: {e}")
# Fallback zu PIL
try:
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
image_with_text = image.copy()
draw = ImageDraw.Draw(image_with_text)
draw.text((text_x, text_y), text, fill="white")
return image_with_text
except:
return image
def create_text_preview(image, text, text_x, text_y, font_size="80px", font_family="Standard", font_color="Weiß"):
"""Vorschau mit OpenCV"""
if image is None:
return image
try:
# ✅ PRÜFE OB KOORDINATEN GESETZT SIND
if text_x is None or text_y is None:
print("Keine Koordinaten für Vorschau gesetzt")
return image
# Zu OpenCV konvertieren
if isinstance(image, np.ndarray):
opencv_image = image
else:
opencv_image = np.array(image)
opencv_image = cv2.cvtColor(opencv_image, cv2.COLOR_RGB2BGR)
preview = opencv_image.copy()
# Rote Marker
if text and text_x is not None and text_y is not None and (text_x != 100 or text_y != 100):
cv2.circle(preview, (int(text_x), int(text_y)), 8, (0, 0, 255), -1)
# Text zeichnen
if text:
try:
pixel_size = int(font_size.replace("px", ""))
font_scale = pixel_size / 30.0
except:
font_scale = 2.0
color_mapping = {
"Weiß": (255, 255, 255),
"Schwarz": (0, 0, 0),
"Rot": (0, 0, 255)
}
actual_color = color_mapping.get(font_color, (255, 255, 255))
# OpenCV Font basierend auf Auswahl
font_mapping = {
"Standard": cv2.FONT_HERSHEY_SIMPLEX,
"Einfache Handschrift": cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
"Verschnörkelte Handschrift": cv2.FONT_HERSHEY_SCRIPT_COMPLEX
}
font = font_mapping.get(font_family, cv2.FONT_HERSHEY_SIMPLEX)
thickness = max(2, int(pixel_size / 20))
cv2.putText(
preview,
text,
(int(text_x), int(text_y)),
font,
font_scale,
actual_color,
thickness,
cv2.LINE_AA
)
print(f"OpenCV Vorschau: Schrift {font_family}, Größe {font_scale:.2f}")
# Zurück zu PIL
result_image = cv2.cvtColor(preview, cv2.COLOR_BGR2RGB)
return Image.fromarray(result_image)
except Exception as e:
print(f"Fehler bei OpenCV Vorschau: {e}")
return image
def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, font_size, font_family, font_color, target_selector):
"""Aktualisiert die Text-Vorschau für Bild-zu-Bild mit Formatierung"""
print(f"Update Vorschau I2I - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}, Ziel: {target_selector}")
# ✅ PRÜFE OB KOORDINATEN GESETZT SIND
if text_x is None or text_y is None:
print("Keine Koordinaten gesetzt, zeige Originalbild")
if target_selector == "Originalbild":
return original_image if original_image is not None else generated_image
else:
return generated_image if generated_image is not None else original_image
# Wähle Zielbild
if target_selector == "Originalbild":
target_image = original_image
else:
target_image = generated_image
if target_image is None:
print("Kein Zielbild verfügbar")
return original_image if original_image is not None else generated_image
# ✅ FEHLERBEHANDLUNG FÜR OPENCV
try:
return create_text_preview(target_image, text, text_x, text_y, font_size, font_family, font_color)
except Exception as e:
print(f"Fehler bei OpenCV Vorschau I2I: {e}")
return target_image
def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
"""Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
print(f"Update Vorschau T2I - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}")
# ✅ PRÜFE OB KOORDINATEN GESETZT SIND
if text_x is None or text_y is None:
print("Keine Koordinaten gesetzt, zeige Originalbild")
return image
if image is None:
print("Kein Bild verfügbar")
return None
# ✅ FEHLERBEHANDLUNG FÜR OPENCV
try:
return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)
except Exception as e:
print(f"Fehler bei OpenCV Vorschau T2I: {e}")
return image
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]
return 100, 100 # ✅ Standardwerte statt None
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():
with gr.Column():
font_size_t2i = gr.Dropdown(
choices=["120px", "80px", "40px"],
value="80px",
label="Schriftgröße"
)
with gr.Column():
font_family_t2i = gr.Dropdown(
choices=["Standard", "Einfache Handschrift", "Verschnörkelte Handschrift"],
value="Standard",
label="Schriftart"
)
with gr.Column():
font_color_t2i = gr.Dropdown(
choices=["Weiß", "Schwarz", "Rot"],
value="Weiß",
label="Schriftfarbe"
)
with gr.Row():
# ✅ ÄNDERUNG: Standardwerte statt None
text_x_t2i = gr.Number(value=100, visible=False)
text_y_t2i = gr.Number(value=100, 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, font_size_t2i, font_family_t2i, font_color_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():
# ✅ ÄNDERUNG: Standardwerte statt None
text_x_i2i = gr.Number(value=100, visible=False)
text_y_i2i = gr.Number(value=100, 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
|