Astridkraft commited on
Commit
d6bf45b
·
verified ·
1 Parent(s): 117b23e

Update text_integration.py

Browse files
Files changed (1) hide show
  1. text_integration.py +76 -36
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="36px", font_family="Arial", font_color="Weiß"):
6
  """Fügt Text mit PIL zum Bild hinzu mit Formatierungsoptionen"""
7
  if image is None or not text:
8
  return image
@@ -15,12 +15,12 @@ def add_text_to_image(image, text, text_x, text_y, font_size="36px", font_family
15
  image_with_text = image.copy()
16
  draw = ImageDraw.Draw(image_with_text)
17
 
18
- # Schriftgröße direkt aus dem Dropdown-Wert extrahieren (z.B. "36px" -> 36)
19
  try:
20
  pixel_size = int(font_size.replace("px", ""))
21
  print(f"Schriftgröße direkt: {font_size} -> {pixel_size}px")
22
  except:
23
- pixel_size = 36 # Fallback
24
 
25
  # Schriftfarbe Mapping - schwarz, weiß, rot
26
  color_mapping = {
@@ -32,26 +32,50 @@ def add_text_to_image(image, text, text_x, text_y, font_size="36px", font_family
32
  actual_color = color_mapping.get(font_color, "white")
33
  print(f"Schriftfarbe Mapping: {font_color} -> {actual_color}")
34
 
35
- # Schriftart basierend auf Auswahl
36
- font_paths = {
37
- "Arial": "arial.ttf",
38
- "Times New Roman": "times.ttf",
39
- "Courier New": "cour.ttf"
40
- }
41
-
42
  try:
43
- font_path = font_paths.get(font_family, "arial.ttf")
44
- font = ImageFont.truetype(font_path, pixel_size)
45
- print(f"Schriftart geladen: {font_path} mit Größe {pixel_size}px")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  except Exception as font_error:
47
  print(f"Fehler beim Laden der Schriftart: {font_error}")
48
  # Fallback auf Default Font
49
- try:
50
- font = ImageFont.truetype("arial.ttf", pixel_size)
51
- except:
52
- font = ImageFont.load_default()
53
- print("Fallback auf Default Font")
54
 
 
55
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
56
  print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {pixel_size}px, Schrift: {font_family}, Farbe: {actual_color}")
57
  return image_with_text
@@ -60,7 +84,7 @@ def add_text_to_image(image, text, text_x, text_y, font_size="36px", font_family
60
  print(f"Fehler beim Text hinzufügen: {e}")
61
  return image
62
 
63
- def create_text_preview(image, text, text_x, text_y, font_size="36px", font_family="Arial", font_color="Weiß"):
64
  """Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
65
  if image is None:
66
  return image
@@ -92,7 +116,7 @@ def create_text_preview(image, text, text_x, text_y, font_size="36px", font_fami
92
  try:
93
  pixel_size = int(font_size.replace("px", ""))
94
  except:
95
- pixel_size = 36
96
 
97
  # Schriftfarbe Mapping für Vorschau
98
  color_mapping = {
@@ -103,23 +127,38 @@ def create_text_preview(image, text, text_x, text_y, font_size="36px", font_fami
103
 
104
  actual_color = color_mapping.get(font_color, "white")
105
 
106
- # Schriftart basierend auf Auswahl
107
- font_paths = {
108
- "Arial": "arial.ttf",
109
- "Times New Roman": "times.ttf",
110
- "Courier New": "cour.ttf"
111
- }
112
-
113
  try:
114
- font_path = font_paths.get(font_family, "arial.ttf")
115
- font = ImageFont.truetype(font_path, pixel_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  except Exception as font_error:
117
  print(f"Fehler beim Laden der Schriftart für Vorschau: {font_error}")
118
- # Fallback auf Default Font
119
- try:
120
- font = ImageFont.truetype("arial.ttf", pixel_size)
121
- except:
122
- font = ImageFont.load_default()
123
 
124
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
125
  print(f"Vorschau: Text mit Größe {pixel_size}px, Farbe {actual_color} gezeichnet")
@@ -130,6 +169,7 @@ def create_text_preview(image, text, text_x, text_y, font_size="36px", font_fami
130
  print(f"Fehler bei Text-Vorschau: {e}")
131
  return image
132
 
 
133
  def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, target_selector):
134
  """Aktualisiert die Text-Vorschau für Bild-zu-Bild"""
135
  if target_selector == "Originalbild":
@@ -170,8 +210,8 @@ def create_text_integration_section_t2i():
170
  with gr.Row():
171
  with gr.Column():
172
  font_size_t2i = gr.Dropdown(
173
- choices=["48px", "36px", "24px"],
174
- value="36px",
175
  label="Schriftgröße"
176
  )
177
  with gr.Column():
 
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="80px", font_family="Arial", font_color="Weiß"):
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 direkt aus dem Dropdown-Wert extrahieren (z.B. "80px" -> 80)
19
  try:
20
  pixel_size = int(font_size.replace("px", ""))
21
  print(f"Schriftgröße direkt: {font_size} -> {pixel_size}px")
22
  except:
23
+ pixel_size = 80 # Fallback - HÖHERER WERT
24
 
25
  # Schriftfarbe Mapping - schwarz, weiß, rot
26
  color_mapping = {
 
32
  actual_color = color_mapping.get(font_color, "white")
33
  print(f"Schriftfarbe Mapping: {font_color} -> {actual_color}")
34
 
35
+ # VERBESSERT: Größere Schriftarten erzwingen
 
 
 
 
 
 
36
  try:
37
+ if font_family == "Arial":
38
+ # Versuche verschiedene Arial-Varianten
39
+ try:
40
+ font = ImageFont.truetype("arial.ttf", pixel_size)
41
+ except:
42
+ try:
43
+ font = ImageFont.truetype("Arial.ttf", pixel_size)
44
+ except:
45
+ try:
46
+ font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", pixel_size)
47
+ except:
48
+ # Finaler Fallback mit Default-Font
49
+ font = ImageFont.load_default()
50
+ print(f"Verwende Default-Schriftart mit Größe {pixel_size}")
51
+ elif font_family == "Times New Roman":
52
+ try:
53
+ font = ImageFont.truetype("times.ttf", pixel_size)
54
+ except:
55
+ try:
56
+ font = ImageFont.truetype("Times New Roman.ttf", pixel_size)
57
+ except:
58
+ font = ImageFont.load_default()
59
+ elif font_family == "Courier New":
60
+ try:
61
+ font = ImageFont.truetype("cour.ttf", pixel_size)
62
+ except:
63
+ try:
64
+ font = ImageFont.truetype("Courier New.ttf", pixel_size)
65
+ except:
66
+ font = ImageFont.load_default()
67
+ else:
68
+ font = ImageFont.load_default()
69
+
70
+ print(f"Schriftart geladen: {font_family} mit Größe {pixel_size}px")
71
+
72
  except Exception as font_error:
73
  print(f"Fehler beim Laden der Schriftart: {font_error}")
74
  # Fallback auf Default Font
75
+ font = ImageFont.load_default()
76
+ print("Fallback auf Default Font")
 
 
 
77
 
78
+ # Text zeichnen
79
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
80
  print(f"Text '{text}' an Position ({text_x}, {text_y}) hinzugefügt - Größe: {pixel_size}px, Schrift: {font_family}, Farbe: {actual_color}")
81
  return image_with_text
 
84
  print(f"Fehler beim Text hinzufügen: {e}")
85
  return image
86
 
87
+ def create_text_preview(image, text, text_x, text_y, font_size="80px", font_family="Arial", font_color="Weiß"):
88
  """Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
89
  if image is None:
90
  return image
 
116
  try:
117
  pixel_size = int(font_size.replace("px", ""))
118
  except:
119
+ pixel_size = 80 # HÖHERER FALLBACK
120
 
121
  # Schriftfarbe Mapping für Vorschau
122
  color_mapping = {
 
127
 
128
  actual_color = color_mapping.get(font_color, "white")
129
 
130
+ # Gleiche Schriftart-Logik wie in add_text_to_image
 
 
 
 
 
 
131
  try:
132
+ if font_family == "Arial":
133
+ try:
134
+ font = ImageFont.truetype("arial.ttf", pixel_size)
135
+ except:
136
+ try:
137
+ font = ImageFont.truetype("Arial.ttf", pixel_size)
138
+ except:
139
+ font = ImageFont.load_default()
140
+ elif font_family == "Times New Roman":
141
+ try:
142
+ font = ImageFont.truetype("times.ttf", pixel_size)
143
+ except:
144
+ try:
145
+ font = ImageFont.truetype("Times New Roman.ttf", pixel_size)
146
+ except:
147
+ font = ImageFont.load_default()
148
+ elif font_family == "Courier New":
149
+ try:
150
+ font = ImageFont.truetype("cour.ttf", pixel_size)
151
+ except:
152
+ try:
153
+ font = ImageFont.truetype("Courier New.ttf", pixel_size)
154
+ except:
155
+ font = ImageFont.load_default()
156
+ else:
157
+ font = ImageFont.load_default()
158
+
159
  except Exception as font_error:
160
  print(f"Fehler beim Laden der Schriftart für Vorschau: {font_error}")
161
+ font = ImageFont.load_default()
 
 
 
 
162
 
163
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
164
  print(f"Vorschau: Text mit Größe {pixel_size}px, Farbe {actual_color} gezeichnet")
 
169
  print(f"Fehler bei Text-Vorschau: {e}")
170
  return image
171
 
172
+ # Die restlichen Funktionen bleiben unverändert...
173
  def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, target_selector):
174
  """Aktualisiert die Text-Vorschau für Bild-zu-Bild"""
175
  if target_selector == "Originalbild":
 
210
  with gr.Row():
211
  with gr.Column():
212
  font_size_t2i = gr.Dropdown(
213
+ choices=["120px", "80px", "40px"], # DEUTLICH HÖHERE WERTE
214
+ value="80px",
215
  label="Schriftgröße"
216
  )
217
  with gr.Column():