Spaces:
Sleeping
Sleeping
Update text_integration.py
Browse files- text_integration.py +13 -26
text_integration.py
CHANGED
|
@@ -2,7 +2,7 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="
|
| 6 |
"""Fügt Text mit PIL zum Bild hinzu mit Formatierungsoptionen"""
|
| 7 |
if image is None or not text:
|
| 8 |
return image
|
|
@@ -15,18 +15,12 @@ def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="
|
|
| 15 |
image_with_text = image.copy()
|
| 16 |
draw = ImageDraw.Draw(image_with_text)
|
| 17 |
|
| 18 |
-
# Schriftgröße
|
|
|
|
| 19 |
try:
|
| 20 |
-
pixel_size = int(font_size)
|
| 21 |
except:
|
| 22 |
-
pixel_size = 90
|
| 23 |
-
|
| 24 |
-
# Farbnamen in Hex-Codes umwandeln
|
| 25 |
-
color_mapping = {
|
| 26 |
-
"Weiß": "#FFFFFF",
|
| 27 |
-
"Schwarz": "#000000"
|
| 28 |
-
}
|
| 29 |
-
hex_color = color_mapping.get(font_color, "#FFFFFF")
|
| 30 |
|
| 31 |
# Schriftart basierend auf Auswahl
|
| 32 |
font_paths = {
|
|
@@ -38,7 +32,7 @@ def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="
|
|
| 38 |
try:
|
| 39 |
font_path = font_paths.get(font_family, "arial.ttf")
|
| 40 |
font = ImageFont.truetype(font_path, pixel_size)
|
| 41 |
-
print(f"Schriftart geladen: {font_path} mit Größe {pixel_size}
|
| 42 |
except Exception as font_error:
|
| 43 |
print(f"Fehler beim Laden der Schriftart: {font_error}")
|
| 44 |
# Fallback auf Default Font
|
|
@@ -48,15 +42,15 @@ def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="
|
|
| 48 |
font = ImageFont.load_default()
|
| 49 |
print("Fallback auf Default Font")
|
| 50 |
|
| 51 |
-
draw.text((text_x, text_y), text, fill=
|
| 52 |
-
print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {pixel_size}px, Schrift: {font_family}, Farbe: {font_color}
|
| 53 |
return image_with_text
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Fehler beim Text hinzufügen: {e}")
|
| 57 |
return image
|
| 58 |
|
| 59 |
-
def create_text_preview(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="
|
| 60 |
"""Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
|
| 61 |
if image is None:
|
| 62 |
return image
|
|
@@ -84,19 +78,12 @@ def create_text_preview(image, text, text_x, text_y, font_size="90", font_family
|
|
| 84 |
|
| 85 |
# Text zeichnen wenn vorhanden
|
| 86 |
if text:
|
| 87 |
-
# Schriftgröße direkt verwenden
|
| 88 |
try:
|
| 89 |
pixel_size = int(font_size)
|
| 90 |
except:
|
| 91 |
pixel_size = 90
|
| 92 |
|
| 93 |
-
# Farbnamen in Hex-Codes umwandeln
|
| 94 |
-
color_mapping = {
|
| 95 |
-
"Weiß": "#FFFFFF",
|
| 96 |
-
"Schwarz": "#000000"
|
| 97 |
-
}
|
| 98 |
-
hex_color = color_mapping.get(font_color, "#FFFFFF")
|
| 99 |
-
|
| 100 |
# Schriftart basierend auf Auswahl
|
| 101 |
font_paths = {
|
| 102 |
"Arial": "arial.ttf",
|
|
@@ -115,8 +102,8 @@ def create_text_preview(image, text, text_x, text_y, font_size="90", font_family
|
|
| 115 |
except:
|
| 116 |
font = ImageFont.load_default()
|
| 117 |
|
| 118 |
-
draw.text((text_x, text_y), text, fill=
|
| 119 |
-
print(f"Vorschau: Text mit Größe {pixel_size}px
|
| 120 |
|
| 121 |
return preview
|
| 122 |
|
|
@@ -135,7 +122,7 @@ def update_text_preview_i2i(original_image, generated_image, text, text_x, text_
|
|
| 135 |
|
| 136 |
def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
|
| 137 |
"""Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
|
| 138 |
-
print(f"Update Vorschau - Größe: {font_size}
|
| 139 |
return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)
|
| 140 |
|
| 141 |
def capture_click(event: gr.SelectData):
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
def add_text_to_image(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="#FFFFFF"):
|
| 6 |
"""Fügt Text mit PIL zum Bild hinzu mit Formatierungsoptionen"""
|
| 7 |
if image is None or not text:
|
| 8 |
return image
|
|
|
|
| 15 |
image_with_text = image.copy()
|
| 16 |
draw = ImageDraw.Draw(image_with_text)
|
| 17 |
|
| 18 |
+
# Schriftgröße anpassen - direkte Verwendung der ausgewählten Größe
|
| 19 |
+
# Für 512px Bilder sind diese Größen angemessen
|
| 20 |
try:
|
| 21 |
+
pixel_size = int(font_size) # Direkt die ausgewählte Größe verwenden
|
| 22 |
except:
|
| 23 |
+
pixel_size = 90 # Fallback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Schriftart basierend auf Auswahl
|
| 26 |
font_paths = {
|
|
|
|
| 32 |
try:
|
| 33 |
font_path = font_paths.get(font_family, "arial.ttf")
|
| 34 |
font = ImageFont.truetype(font_path, pixel_size)
|
| 35 |
+
print(f"Schriftart geladen: {font_path} mit Größe {pixel_size}")
|
| 36 |
except Exception as font_error:
|
| 37 |
print(f"Fehler beim Laden der Schriftart: {font_error}")
|
| 38 |
# Fallback auf Default Font
|
|
|
|
| 42 |
font = ImageFont.load_default()
|
| 43 |
print("Fallback auf Default Font")
|
| 44 |
|
| 45 |
+
draw.text((text_x, text_y), text, fill=font_color, font=font)
|
| 46 |
+
print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {pixel_size}px, Schrift: {font_family}, Farbe: {font_color}")
|
| 47 |
return image_with_text
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Fehler beim Text hinzufügen: {e}")
|
| 51 |
return image
|
| 52 |
|
| 53 |
+
def create_text_preview(image, text, text_x, text_y, font_size="90", font_family="Arial", font_color="#FFFFFF"):
|
| 54 |
"""Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
|
| 55 |
if image is None:
|
| 56 |
return image
|
|
|
|
| 78 |
|
| 79 |
# Text zeichnen wenn vorhanden
|
| 80 |
if text:
|
| 81 |
+
# Schriftgröße direkt verwenden
|
| 82 |
try:
|
| 83 |
pixel_size = int(font_size)
|
| 84 |
except:
|
| 85 |
pixel_size = 90
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
# Schriftart basierend auf Auswahl
|
| 88 |
font_paths = {
|
| 89 |
"Arial": "arial.ttf",
|
|
|
|
| 102 |
except:
|
| 103 |
font = ImageFont.load_default()
|
| 104 |
|
| 105 |
+
draw.text((text_x, text_y), text, fill=font_color, font=font)
|
| 106 |
+
print(f"Vorschau: Text mit Größe {pixel_size}px gezeichnet")
|
| 107 |
|
| 108 |
return preview
|
| 109 |
|
|
|
|
| 122 |
|
| 123 |
def update_text_preview_t2i(image, text, text_x, text_y, font_size, font_family, font_color):
|
| 124 |
"""Aktualisiert die Text-Vorschau für Text-zu-Bild mit Formatierung"""
|
| 125 |
+
print(f"Update Vorschau - Größe: {font_size}, Schrift: {font_family}, Farbe: {font_color}")
|
| 126 |
return create_text_preview(image, text, text_x, text_y, font_size, font_family, font_color)
|
| 127 |
|
| 128 |
def capture_click(event: gr.SelectData):
|