Astridkraft commited on
Commit
53ff9a3
·
verified ·
1 Parent(s): d6bf45b

Update text_integration.py

Browse files
Files changed (1) hide show
  1. text_integration.py +40 -75
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="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
@@ -20,7 +20,7 @@ def add_text_to_image(image, text, text_x, text_y, font_size="80px", font_family
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,48 +32,28 @@ def add_text_to_image(image, text, text_x, text_y, font_size="80px", font_family
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)
@@ -84,7 +64,7 @@ def add_text_to_image(image, text, text_x, text_y, font_size="80px", font_family
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,7 +96,7 @@ def create_text_preview(image, text, text_x, text_y, font_size="80px", font_fami
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,38 +107,24 @@ def create_text_preview(image, text, text_x, text_y, font_size="80px", font_fami
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,7 +135,6 @@ def create_text_preview(image, text, text_x, text_y, font_size="80px", font_fami
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,14 +175,14 @@ def create_text_integration_section_t2i():
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():
218
  font_family_t2i = gr.Dropdown(
219
- choices=["Arial", "Times New Roman", "Courier New"],
220
- value="Arial",
221
  label="Schriftart"
222
  )
223
  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="Roboto", 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
 
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
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
+ # NEU: 3 garantierte TrueType-Schriftarten mit relativen Pfaden
36
+ font_paths = {
37
+ "Roboto": "./fonts/Roboto-Regular.ttf",
38
+ "Open Sans": "./fonts/OpenSans-Regular.ttf",
39
+ "DejaVu Sans": "./fonts/DejaVuSans.ttf"
40
+ }
41
+
42
  try:
43
+ font_path = font_paths.get(font_family, "./fonts/Roboto-Regular.ttf")
44
+ font = ImageFont.truetype(font_path, pixel_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  print(f"Schriftart geladen: {font_family} mit Größe {pixel_size}px")
46
 
47
  except Exception as font_error:
48
+ print(f"Fehler beim Laden der Schriftart {font_family}: {font_error}")
49
+ # Fallback: Versuche die erste verfügbare Schriftart
50
+ try:
51
+ font = ImageFont.truetype("./fonts/Roboto-Regular.ttf", pixel_size)
52
+ print(f"Fallback auf Roboto mit Größe {pixel_size}px")
53
+ except:
54
+ # Letzter Fallback - sollte nicht passieren wenn Schriftarten vorhanden sind
55
+ font = ImageFont.load_default()
56
+ print(f"KRITISCH: Keine TrueType-Schriftart gefunden! Verwende Default (nicht skalierbar)")
57
 
58
  # Text zeichnen
59
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
 
64
  print(f"Fehler beim Text hinzufügen: {e}")
65
  return image
66
 
67
+ def create_text_preview(image, text, text_x, text_y, font_size="80px", font_family="Roboto", font_color="Weiß"):
68
  """Erstellt eine Vorschau mit Text und rotem Marker - nur EINEN Punkt"""
69
  if image is None:
70
  return image
 
96
  try:
97
  pixel_size = int(font_size.replace("px", ""))
98
  except:
99
+ pixel_size = 80
100
 
101
  # Schriftfarbe Mapping für Vorschau
102
  color_mapping = {
 
107
 
108
  actual_color = color_mapping.get(font_color, "white")
109
 
110
+ # NEU: Gleiche Schriftart-Logik wie in add_text_to_image
111
+ font_paths = {
112
+ "Roboto": "./fonts/Roboto-Regular.ttf",
113
+ "Open Sans": "./fonts/OpenSans-Regular.ttf",
114
+ "DejaVu Sans": "./fonts/DejaVuSans.ttf"
115
+ }
116
+
117
  try:
118
+ font_path = font_paths.get(font_family, "./fonts/Roboto-Regular.ttf")
119
+ font = ImageFont.truetype(font_path, pixel_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  except Exception as font_error:
121
  print(f"Fehler beim Laden der Schriftart für Vorschau: {font_error}")
122
+ # Fallback
123
+ try:
124
+ font = ImageFont.truetype("./fonts/Roboto-Regular.ttf", pixel_size)
125
+ except:
126
+ font = ImageFont.load_default()
127
+ print("WARNUNG: Fallback auf nicht-skalierbare Default-Schriftart")
128
 
129
  draw.text((text_x, text_y), text, fill=actual_color, font=font)
130
  print(f"Vorschau: Text mit Größe {pixel_size}px, Farbe {actual_color} gezeichnet")
 
135
  print(f"Fehler bei Text-Vorschau: {e}")
136
  return image
137
 
 
138
  def update_text_preview_i2i(original_image, generated_image, text, text_x, text_y, target_selector):
139
  """Aktualisiert die Text-Vorschau für Bild-zu-Bild"""
140
  if target_selector == "Originalbild":
 
175
  with gr.Row():
176
  with gr.Column():
177
  font_size_t2i = gr.Dropdown(
178
+ choices=["120px", "80px", "40px"],
179
  value="80px",
180
  label="Schriftgröße"
181
  )
182
  with gr.Column():
183
  font_family_t2i = gr.Dropdown(
184
+ choices=["Roboto", "Open Sans", "DejaVu Sans"], # NEU: 3 TrueType-Schriftarten
185
+ value="Roboto",
186
  label="Schriftart"
187
  )
188
  with gr.Column():